Constexpr
constexpr is a C++ or C language specifier that allows entities or expressions to satisfy compile-time constant-evaluation requirements.
Learn C++ by Example
constexpr is a C++ specifier for variables and functions that permits evaluation at compile time when their initialization or arguments satisfy the required constant-expression rules.
- How it is used
- Uses compile-time evaluation to check properties early, reduce runtime work, and make suitable calculations testable.
- How deep it goes
- Explains variables, function arguments, static_assert, and evolving language support, but defers detailed constant-expression rules.
- Examples
- Demonstrates prime checks, triangle-number vectors, shuffled slot-machine reels, and input parsing with optional results.
- What you can do after
- Evaluate and test suitable C++ functions at compile time, while retaining runtime calls when inputs are unavailable earlier.
Modern C, Third Edition
constexpr is a C23 language construct for declaring entities whose values or initialization satisfy compile-time constant-expression requirements.
- How it is used
- Uses constexpr to provide named or unnamed constants for compile-time expressions and file-scope initialization.
- How deep it goes
- Explains read-only guarantees, exact type fitting, integer constant-expression use, compound literals, and possible storage optimization.
- Examples
- Demonstrates π, an invalid unsigned conversion, a corvid-name array compound literal, and constexpr operands in enumeration expressions.
- What you can do after
- Evaluate/understand which C23 objects and expressions qualify as compile-time constants and diagnose value-changing initializations.
100 C++ Mistakes and How to Avoid Them
constexpr is a C++ specifier that permits a variable or function to participate in compile-time constant evaluation when its requirements are satisfied.
- How it is used
- Uses compile-time constant evaluation to replace potentially unsafe macro definitions.
- How deep it goes
- Defines compile-time evaluation and type guarantees but does not detail constexpr function requirements or implementation internals.
- Examples
- Uses a PI constant and contrasts macro-based code with C++ constructs that involve compiler checking.
- What you can do after
- Apply constexpr to express compile-time values while exposing type mismatches to the compiler.
Read how 100 C++ Mistakes and How to Avoid Them explains constexpr →
Related concepts
- read-only objectproduces · A read-only object is an object that a program is not permitted to modify through its applicable access or type qualification.
- compound literaluses · A compound literal is a C expression consisting of a parenthesized type name followed by an initializer that creates an unnamed object.
- static_assertuses · A static assertion is a compile-time check requiring a constant condition to be true and otherwise producing a diagnostic.
- const qualifierproduces · A language feature that marks a value, binding, or access path as constant, restricting reassignment or modification according to the language’s rules.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


