IT lexicon › Programming › constexpr

constexpr

Programming På svenska → Updated: 2026-07-29

The keyword saying something may be evaluated at compile time. On a function it means may, not must — the source of most of the confusion.

A constexpr function called with constant arguments in a context requiring a constant is evaluated by the compiler; called with ordinary variables it runs like any other function at runtime. Same code, two lives. The possibilities have widened by stages: C++11 permitted only a single return statement, C++14 allowed loops, branches and local variables, and C++20 permitted dynamic allocation and virtual calls during constant evaluation along with std::is_constant_evaluated() for branching on which context you are in. To require compile time there is consteval, and constinit guarantees compile-time initialisation without making the value const. In practice the trend means lookup tables and parsing have moved into the build.

← Back to the lexicon