Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Intermediate
minutes
Constexpr Terminology
Overview of compile-time function evaluation concepts in this chapter.
Last updated:
Compile-Time Function Evaluation Terminology Reference
This reference provides an overview of compile-time evaluation terminology you'll encounter in this chapter. Use it as a quick lookup guide.
Compile-Time Evaluation Basics
| Term | Definition | Example |
|---|---|---|
| Compile-Time Evaluation | Code executed during compilation, not at runtime | Computing constants before program runs |
| Runtime Evaluation | Code executed when program runs | Normal function calls during execution |
| Constant Expression | Expression that can be evaluated at compile time | 5 + 3 or constexpr function result |
| Compile-Time Constant | Value known and fixed at compile time | constexpr int max = 100; |
constexpr Functions
| Term | Definition | Example |
|---|---|---|
| constexpr Function | Function that can be evaluated at compile time | constexpr int square(int x) { return x * x; } |
| constexpr Variable | Variable initialized with compile-time constant | constexpr int size = 10; |
| Constant Expression Context | Where compile-time constants are required | Array size, template arguments |
| Runtime Context | Normal execution environment | Regular function calls |
constexpr Function Rules
| Term | Definition | Example |
|---|---|---|
| Literal Type | Type that can be used in constexpr context | int, double, char, simple classes |
| Constexpr-Compatible | Function body meets constexpr requirements | No static variables, no undefined behavior |
| Return Statement | Statement that returns value from function | return x * x; |
| Single Return Path | constexpr function with one return statement | Older C++ requirement, relaxed in modern C++ |
Advanced constexpr Features
| Term | Definition | Example |
|---|---|---|
| constexpr if | Compile-time conditional statement | if constexpr (sizeof(T) > 4) |
| constexpr Constructor | Constructor that can run at compile time | Class initialization in constant expressions |
| constexpr Member Function | Class method that can be evaluated at compile time | constexpr int size() const { return m_size; } |
| constexpr Lambda | Lambda function marked constexpr | auto f = [](int x) constexpr { return x * 2; }; |
consteval Functions
| Term | Definition | Example |
|---|---|---|
| consteval Function | Function that must be evaluated at compile time | consteval int compute(int x) { return x * 2; } |
| Immediate Function | Another name for consteval function | Always produces compile-time constant |
| Mandatory Compile-Time | Function cannot be called at runtime | Compile error if runtime evaluation attempted |
| consteval vs constexpr | consteval is always compile-time, constexpr can be either | consteval is stricter requirement |
Compile-Time Optimization
| Term | Definition | Example |
|---|---|---|
| Constant Folding | Compiler evaluating constant expressions | int x = 5 + 3; becomes int x = 8; |
| Inline Expansion | Replacing function call with function body | Compiler inserts code directly |
| Zero Runtime Cost | Code executed at compile time has no runtime overhead | Pre-computed values in executable |
| Compile-Time Computation | Calculations performed during compilation | Computing lookup tables at compile time |
constexpr Contexts
| Term | Definition | Example |
|---|---|---|
| Array Bounds | Size specification for arrays | int arr[constexpr_size]; |
| Template Arguments | Values passed to templates | std::array<int, constexpr_size> |
| Case Labels | Values in switch statement cases | case constexpr_value: |
| Enumerator Values | Values assigned to enum members | enum { Max = constexpr_value }; |
constexpr Limitations
| Term | Definition | Example |
|---|---|---|
| Non-Literal Type | Type that cannot be used in constexpr | Types with virtual functions |
| Undefined Behavior | Operations with no defined meaning | Dereferencing null pointer |
| Static Variable | Variable with static storage duration | Not allowed in constexpr functions |
| Dynamic Memory | Heap allocation with new/delete | Not allowed in most constexpr contexts |
Best Practices
| Term | Definition | Example |
|---|---|---|
| constexpr by Default | Make functions constexpr when possible | Enables compile-time optimization |
| Dual-Mode Function | Function usable at compile and runtime | constexpr functions can do both |
| Compile-Time Check | Using static_assert with constexpr | static_assert(constexpr_func() > 0); |
| Precomputed Value | Result calculated at compile time | Lookup tables, mathematical constants |
Constexpr Terminology - Quiz
Test your understanding of the lesson.
6 questions
10 minutes
60% to pass
Lesson Discussion
Share your thoughts and questions
💬
No comments yet. Be the first to share your thoughts!