assert in constexpr function

Viewed 568

In trying to work out why I was getting a certain compile error, I came up with the following minimal example:

constexpr void Test(bool test)
{
    if (test)
        return;

    assert(false);
}

This compiles without issue with every version of clang I tried (3.7+), but fails with gcc (tested 5-8), with

error: call to non-‘constexpr’ function ‘void __assert_fail(const char*, const char*, unsigned int, const char*)’

From my understanding, the function should be able to be constexpr because there is a set of argument values for which the function can be evaluated at compile time.

Is my understanding wrong, or is gcc incorrect in failing to compile this?

2 Answers

If you like me have this issue, but you can't upgrade the compiler nor change the code. A quick fix is to have the compiler remove the asserts by adding the NDEBUG flag to the compiler:

  • gcc/clang: -DNDEBUG
  • msvc: /DNDEBUG

Maybe this can save someone a bit of time.

Related