constexpr specifier (since C23)
From cppreference.com
A scalar object declared with the constexpr
storage-class specifier is a constant
constant expression
Additionally, the constant expression that is used for the initializer of such a constant is checked at compile time.
An initializer of floating-point type must be evaluated with the translation-time floating-point environment.
There are some restrictions on the type of an object that can be declared with constexpr
. Namely, the following constructs are not allowed to be constexpr
:
- Pointers (except that null pointers can be
constexpr
), - Variably modified types,
- Atomic types,
volatile
types,restrict
pointers.
Keywords
Notes
Example
Run this code
#include <fenv.h> #include <stdio.h> int main(void) { constexpr float f = 23.0f; constexpr float g = 33.0f; fesetround(FE_TOWARDZERO); constexpr float h = f / g; // is not affected by fesetround() above printf("%f\n", h); }
Output:
0.696969
References
- C23 standard (ISO/IEC 9899:2024):
- TBD TBD (p: TBD)
See also
C++ documentation for
constexpr type specifier |