C++ Logo

std-proposals

Advanced search

[std-proposals] Allow assert in constexpr functions

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 26 Mar 2025 14:40:37 +0000
Today my compiler gave me the following output:

md5.hpp:244:21: error: call to non-‘constexpr’ function ‘void
__assert_fail(const char*, const char*, unsigned int, const char*)’
  244 | assert( 0u == (*ptr >> 8u) );


We should be able to use 'assert' in a constexpr function. If the
function is invoked at compile time (instead of runtime), then it
should be as if the 'assert' was never there. This would be the
easiest way of doing it.

A more complicated way of doing it would be to get the compiler at
compile time to actually evaluate the assertion, and to cease
compiling if the assertion fails -- this would be ideal.

Received on 2025-03-26 14:40:49