I have some rather costly static_assert calls scattered throughout my code. While these are valuable, they are often superfulous and significantly contribute to compile time and memory usage.
Can I disable them?
Wrap them in the standard NDEBUG macro.
#ifndef NDEBUG static_assert(...); #endif That way for release builds you can disable them just like regular assert. Although I don't really see a purpose to this.
If you don't like wrapping up the calls in a macro, you can define a macro that does it for you:
#ifndef STATIC_ASSERT #ifndef NDEBUG #define STATIC_ASSERT(...) static_assert(__VA_ARGS__) #else #define STATIC_ASSERT(...) static_assert(true, "") #endif // NDEBUG #endif // STATIC_ASSERT Usage is similar to a regular static_assert. Note that if your program defines a keyword and includes a standard library header then it is undefined behaviour to define a static_assert macro.
static_assert) with runtime-checks. If you want to remove one of them, it is quite possible you don't want to disable the others too.NDEBUG due to familiarity.do { } while(0) won't work for static_asserts in namespace scope or in class bodies.You can either wrap them, each for itself, in its own #ifdef:
#ifndef NO_STATIC_ASSERT static_assert(...); #endif Or, you can define your own STATIC_ASSERT:
#ifndef NO_STATIC_ASSERT #define STATIC_ASSERT(...) /**/ #else #define STATIC_ASSERT(...) static_assert(__VA_ARGS__) #endif #define static_assert(...) works too, though it is UB.That way you can remove their influence on compilation-performance (they never had any influence on runtime-performance anyway).
static_assert macro is undefined behaviour
static_assertcalls are responsible for all that overhead?static_asserts is by removing them from the code which will be compiled, for example by using the preprocessor. They do not in any way contribute to the compiled code.