Search code examples
c++cdebuggingassert

Better alternatives to assert(false) in C/C++


Currently, I write

assert(false);

at places that my code is never supposed to reach. One example, in a very C-ish style, is:

int findzero( int length, int * array ) {
  for( int i = 0; i < length; i++ )
    if( array[i] == 0 )
      return i;
  assert(false);
}

My compiler recognizes that the program finishes once assert(false) has been reached. However, whenever I compile with -DNDEBUG for performance reasons, the last assertion vanishes and the compiler warns that the execution finishes the function without a return statement.

What are better alternatives of finishing off a program if a supposedly unreachable part of the code has been reached? The solution should

  • be recognized by the compiler and not produce warnings (like the ones above or others)
  • perhaps even allow for a custom error message.

I am explicitly interested in solutions no matter whether it's modern C++ or like 90s C.


Solution

  • Replacing your assert(false) is exactly what "unreachable" built-ins are for.

    They are a semantic equivalent to your use of assert(false). In fact, VS's is spelt very similarly.

    GCC/Clang/Intel:

    __builtin_unreachable()
    

    MSVS:

     __assume(false)
    

    These have effect regardless of NDEBUG (unlike assert) or optimisation levels.

    Your compiler, particularly with the above built-ins but also possibly with your assert(false), nods its head in understanding that you're promising that part of the function will never be reached. It can use this to perform some optimisations on certain code paths, and it will silence warnings about missing returns because you've already promised that it was deliberate.

    The trade-off is that the statement itself has undefined behaviour (much like going forth and flowing off the end of the function was already). In some situations, you may instead wish to consider throwing an exception (or returning some "error code" value instead), or calling std::abort() (in C++) if you want to just terminate the program.


    There's a proposal (P0627R0), to add this to C++ as a standard attribute.


    From the GCC docs on Builtins:

    If control flow reaches the point of the __builtin_unreachable, the program is undefined. It is useful in situations where the compiler cannot deduce the unreachability of the code. [..]