Constant Expressions

A constant expression is an expression that must be evaluated at compile-time. They are the backbone of compile-time evaluation in C++.

To be able to be evaluated at compile-time, all of it's parts need to be evaluable at compile-time. Constant expression can contain the following:

Even if the return value of a normal (non-constexpr) function is constant, it's never considered a constant expression because functions execute at runtime.

1.1. Constexpr Variables and Functions

When using a const variable, we aren't sure if it's a constant expression or not. For this, we have the constexpr keyword, which guarantees the variable to be a compile-time constant.

With the same keyword we can create constexpr functions. They must evaluate at compile-time when called in a constant expression, otherwise they can also be evaluated at runtime.

1.2. Why the fuck?

For what could you use an expression that is guaranteed to be evaluated at compile time?