constexpr Functions and if constexpr
Evaluate code at compile time with constexpr and branch with if constexpr.
Compile-Time Evaluation
The constexpr keyword tells the compiler "evaluate this at compile time when possible." Compile-time computation has zero runtime cost.
constexpr Variables
A constexpr variable must be initialized with a constant expression. The result is fixed at compile time.
constexpr int BUFFER_SIZE = 1024;
constexpr double PI = 3.14159265358979;All lessons in this course
- Function and Class Templates Revisited
- Template Specialization Partial and Full
- Variadic Templates and Parameter Packs
- constexpr Functions and if constexpr