0Pricing
C++ Academy · Lesson

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

  1. Function and Class Templates Revisited
  2. Template Specialization Partial and Full
  3. Variadic Templates and Parameter Packs
  4. constexpr Functions and if constexpr
← Back to C++ Academy