Skip to content
Latchkey

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

  1. Mark the called function constexpr if its body permits compile-time evaluation.
  2. 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.

Frequently asked questions

What causes ""non-constexpr function" in constant"?
Initializing a constexpr variable or sizing a fixed array fails because the initializer calls a function not marked constexpr.
How do I fix "non-constexpr function" in constant?
Make the callee constexpr

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card