Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Use this whenever compile-time expression is implemented (currently exists in C++ and D). Do not use this tag if the compile-time evaluation property is achieved through template metaprogramming in C++ (in which case use the [template-meta-programming] tag).

A constant expression is a specific form of an expression, which is available in and . Use the tag when constexpr or other form of compile-time expression is implied in the question. The expressions are computed at compile-time.


They're usable in contexts including, but not limited to

  • switch statements

  • C style array sizes

  • Template arguments (only built-in types as of C++17)

The constexpr keyword is often associated with constant-expression. It can be used in two contexts: variables and functions. In case of variables, if the expression is not a constant expression, declaration of the variables will cause compilation error. In usage as a specifier in function declarations, it tells about a function which can potentially be computed at compile time if the arguments are themselves constant expressions.

Most of the time it is used in tandem with template-metaprogramming. Other common usage is compile-time computations. For more info, please see the cppreference pages about constant-expressions and constexpr.