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

前端 未结 16 1795
隐瞒了意图╮
隐瞒了意图╮ 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:37

    With C++17 we can do:

    [[maybe_unused]] int Result = Func();
    

    though it involves a bit of extra typing compared to a assert substitution. See this answer.

    Note: Added this because is the first google hit for "c++ assert unused variable".

提交回复
热议问题