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

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

    Most answers suggest using static_cast(expression) trick in Release builds to suppress the warning, but this is actually suboptimal if your intention is to make checks truly Debug-only. The goals of an assertion macro in question are:

    1. Perform checks in Debug mode
    2. Do nothing in Release mode
    3. Emit no warnings in all cases

    The problem is that void-cast approach fails to reach the second goal. While there is no warning, the expression that you've passed to your assertion macro will still be evaluated. If you, for example, just do a variable check, that is probably not a big deal. But what if you call some function in your assertion check like ASSERT(fetchSomeData() == data); (which is very common in my experience)? The fetchSomeData() function will still be called. It may be fast and simple or it may be not.

    What you really need is not only warning suppression but perhaps more importantly - non-evaluation of the debug-only check expression. This can be achieved with a simple trick that I took from a specialized Assert library:

    void myAssertion(bool checkSuccessful)
    {
       if (!checkSuccessful)
        {
          // debug break, log or what not
        }
    }
    
    #define DONT_EVALUATE(expression)                                    \
       {                                                                 \
          true ? static_cast(0) : static_cast((expression)); \
       }
    
    #ifdef DEBUG
    #  define ASSERT(expression) myAssertion((expression))
    #else
    #  define ASSERT(expression) DONT_EVALUATE((expression))
    #endif // DEBUG
    
    int main()
    {
      int a = 0;
      ASSERT(a == 1);
      ASSERT(performAHeavyVerification());
    
      return 0;
    }
    

    All the magic is in the DONT_EVALUATE macro. It is obvious that at least logically the evaluation of your expression is never needed inside of it. To strengthen that, the C++ standard guarantees that only one of the branches of conditional operator will be evaluated. Here is the quote:

    5.16 Conditional operator [expr.cond]

    logical-or-expression ? expression : assignment-expression

    Conditional expressions group right-to-left. The first expression is contextually converted to bool. It is evaluated and if it is true, the result of the conditional expression is the value of the second expression, otherwise that of the third expression. Only one of these expressions is evaluated.

    I have tested this approach in GCC 4.9.0, clang 3.8.0, VS2013 Update 4, VS2015 Update 4 with the most harsh warning levels. In all cases there are no warnings and the checking expression is never evaluated in Release build (in fact the whole thing is completely optimized away). Bare in mind though that with this approach you will get in trouble really fast if you put expressions that have side effects inside the assertion macro, though this is a very bad practice in the first place.

    Also, I would expect that static analyzers may warn about "result of an expression is always constant" (or something like that) with this approach. I've tested for this with clang, VS2013, VS2015 static analysis tools and got no warnings of that kind.

提交回复
热议问题