Avoiding unused variables warnings when using assert() in a Release build

前端 未结 16 1786
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 09:10

Sometimes a local variable is used for the sole purpose of checking it in an assert(), like so -

int Result = Func();
assert( Result == 1 );
<
16条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 09:56

    I wouldn't be able to give a better answer than this, that addresses that problem, and many more:

    Stupid C++ Tricks: Adventures in assert

    #ifdef NDEBUG
    #define ASSERT(x) do { (void)sizeof(x);} while (0)
    #else
    #include 
    #define ASSERT(x) assert(x)
    #endif
    

提交回复
热议问题