3 ms·
> Wouldn't want to have to rip that code out if I wanted the code to behave on a non-gcc toolchain. I wonder if it can be wrapped in #ifdef ?
by yjftsjthsd-h 4y ago
> Wouldn't want to have to rip that code out if I wanted the code to behave on a non-gcc toolchain.
I wonder if it can be wrapped in #ifdef ?
- loeg 4y agoBetter to define a macro that evaluates to the contract/invariants on GCC and empty on other compilers, rather than copying the ifdef/else/endif logic around to every usage.
- gavinray 4y agoYeah sure, there's nothing preventing you from using "constexpr" so it's compile-time evaluated and inlined: (Please no more macros, it's 202- I mean, 2023!) static constexpr bool DEBUG = true; [[demo::invariant]] [[gnu::used]] void check_invariants() { if constexpr (!DEBUG) return; assert(top >= 0 && top <= MAX_SIZE); }
- celrod 4y agoNote that `assert`s are disabled if you define the macro `NDEBUG`, e.g. https://godbolt.org/z/hMWo8KM7q https://godbolt.org/z/hMWo8KM7q CMake defines the macro in release builds: https://github.com/Kitware/CMake/blob/e1eacbe2c522a8bf9a82afb483fe395140240614/Modules/Compiler/GNU.cmake#L58-L60 https://github.com/Kitware/CMake/blob/e1eacbe2c522a8bf9a82af... Would be nice to have a non-macro solution for controlling behavior at configure time, but the `NDEBUG` macro is basically already your `DEBUG` constexpr.