[c++] Generalized constant expressions

In C++, constant expressions are expressions that can be evaluated at compile time. Prior to C++11, constant expressions were quite limited, but with the introduction of generalized constant expressions, C++ gained the ability to evaluate a wider range of expressions at compile time.

What are constant expressions?

A constant expression is an expression that can be evaluated at compile time. This means that its value is known during compilation and can be used as, for example, the size of an array, the value of a non-type template parameter, or in any other context that requires a constant value.

Generalized constant expressions in C++11 and beyond

With the introduction of C++11, the concept of constant expressions was extended to include more complex expressions, such as function calls, conditional expressions, and more.

Example of a generalized constant expression

constexpr int square(int x) {
    return x * x;
}

int arr[square(5)];  // square(5) is a generalized constant expression

In this example, the constexpr specifier indicates that the function square is a constexpr function, meaning that it can be evaluated at compile time. The call to square(5) in the array declaration is a generalized constant expression as it involves a call to a constexpr function.

Limitations and considerations

While generalized constant expressions expand the capabilities of compile-time evaluation in C++, there are still some limitations to be aware of. For example, in C++11, a constexpr function could only contain a single return statement. This limitation has been relaxed in later versions of C++, but there are still rules and restrictions around what can be evaluated at compile time.

Conclusion

Generalized constant expressions in C++ have expanded the possibilities for compile-time evaluation, allowing for more complex expressions to be evaluated at compile time, leading to potentially more efficient and performant code.

For further details, refer to the C++ documentation on constexpr.