Constant Expressions: constexpr
In this lesson, we will see the usage and implementation of constexpr in Modern C++.
Constant Expressions
With the keyword constexpr
, we define an expression that can be evaluated at compile time. constexpr
can be used for variables, functions, and user-defined types. An expression that is evaluated at compile time has many advantages.
A constant expression does the following:
- It can be evaluated at compile time.
- It gives the compiler deep insight into the code.
- It is implicitly thread-safe.
- It can be constructed in the read-only memory (ROM-able).
Get hands-on with 1400+ tech skills courses.