Constexpr
constexpr is a C++ or C language specifier that allows entities or expressions to satisfy compile-time constant-evaluation requirements.
GPU Programming with Triton
constexpr denotes a value available at compile time rather than only at runtime.
- How it is used
- Uses compile-time constants to specialize Triton GPU kernels, particularly their block size.
- How deep it goes
- Explains specialization and binary caching through a vector-addition kernel, including their consequences for benchmarking.
- Examples
- Contrasts runtime vector length with compile-time block size in a vector-addition kernel.
- What you can do after
- Operate/configure a Triton kernel by choosing which parameters specialize compiled binaries and warming up before timing.
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
- Replaces macro-defined constants with expressions evaluated during compilation and checked for type correctness.
- How deep it goes
- Describes compile-time evaluation and type correctness, but does not explain constexpr requirements or declaration syntax.
- Examples
- Uses a PI constant to illustrate replacing a macro with a typed C++ expression.
- What you can do after
- Understand how compile-time evaluation and type checking can make constants safer than macros.
Read how 100 C++ Mistakes and How to Avoid Them explains constexpr →
Related concepts
- compound literaluses · A compound literal is a C expression consisting of a parenthesized type name followed by an initializer that creates an unnamed object.
- 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.
- constantproduces · A constant is a value or named binding treated as fixed rather than reassigned or modified during a computation, whether this is enforced by a language or followed by convention.
- static_assertuses · A static assertion is a compile-time check requiring a constant condition to be true and otherwise producing a diagnostic.
Questions about constexpr
What is constexpr?
constexpr is a C++ or C language specifier that allows entities or expressions to satisfy compile-time constant-evaluation requirements.
Which Manning books explain constexpr?
GPU Programming with Triton, Learn C++ by Example, Modern C, Third Edition and 100 C++ Mistakes and How to Avoid Them.
What is the difference between constexpr and consteval?
constexpr is a C++ or C language specifier that allows entities or expressions to satisfy compile-time constant-evaluation requirements. consteval is a C++ function specifier that requires every invocation of the function to be evaluated at compile time.
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.



