liveBook · Manning

Constexpr

constexpr is a C++ or C language specifier that allows entities or expressions to satisfy compile-time constant-evaluation requirements.

How 3 Manning books explain it

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.

Read how Learn C++ by Example explains constexpr →

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.

Read how Modern C, Third Edition explains constexpr →

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

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.