So I use preprocessor macro NDEBUG
to enable some checks for my debug build. But I would like to replace it with C constant to use it in noexept
clause and in static if
. I know I can probably achieve it like so:
// in constants.hpp
#ifdef NDEBUG
constexpr bool ndebug = true;
#else
constexpr bool ndebug = false;
#endif
But with NDEBUG
you can, for example, supply -D NDEBUG
flag to compiler, but for certain files you can manually specify #undef NDEBUG
. So parts of code will be compiled with NDEBUG
and parts will be compiled without it. So even within one translation unit there will be parts with defined NDEBUG
and parts without it.
Is it possible with C means to devise something that will 100% conform to this behavior, so in every header file there would be compile-time bool
constant with value based on NDEBUG
?
You can of course create constants with different name in each file, if it is the only way can you at least somehow automate this?
CodePudding user response:
If you use variable or function, you will probably have ODR violation if you mix file with NDEBUG defined or not with those variable/function.
You can though declare a MACRO with a value matching NDEBUG presence.
#ifdef NDEBUG
# define NDEBUG_VALUE true
#else
# define NDEBUG_VALUE false
#endif
to use it in noexept clause and in static if
But still care, as ODR-violation can happen quickly.
All definitions should be identical.