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

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

    We use a macro to specifically indicate when something is unused:

    #define _unused(x) ((void)(x))
    

    Then in your example, you'd have:

    int Result = Func();
    assert( Result == 1 );
    _unused( Result ); // make production build happy
    

    That way (a) the production build succeeds, and (b) it is obvious in the code that the variable is unused by design, not that it's just been forgotten about. This is especially helpful when parameters to a function are not used.

提交回复
热议问题