gcc/clang "call to non-constexpr function" in constant expression in CI
A value used in a constant context (a constexpr variable, array bound, template argument, or static_assert) called a function that is not constexpr, so it cannot be evaluated at compile time.
What this error means
Initializing a constexpr variable or sizing a fixed array fails because the initializer calls a function not marked constexpr.
clang
app.cpp:5:18: error: call to non-'constexpr' function 'int compute()'
5 | constexpr int n = compute();
| ^~~~~~~Common causes
How to fix it
Make the callee constexpr
- Mark the called function constexpr if its body permits compile-time evaluation.
- Otherwise use a non-constexpr variable evaluated at runtime.
clang
constexpr int compute() { return 42; }
constexpr int n = compute();How to prevent it
- Mark functions constexpr when they must be usable in constant expressions, and keep runtime-only logic out of constexpr initializers.
Related guides
gcc/clang "narrowing conversion inside { }" in CIFix gcc/clang "narrowing conversion of X inside { }" in CI - brace initialization forbids implicit narrowing…
gcc/clang "array bound is not an integer constant" in CIFix gcc/clang "array bound is not an integer constant before ']'" in CI - a fixed-size array is sized by a ru…