Compile-time Computations in C++14

By Peter Sommerlad

While in the past C++ provided compile-time computation only very clumsily using templates with value parameters, C++11 introduced the constexpr keyword and allows functions and constants to be computed at compile time. It even introduced a new category of types 'Literal Types' that can be used in such compile-time computations. C++14 further relaxed the restrictions on constexpr functions to allow all reasonable statements that make sense at compile time. As long as at compile-time the computation path of a function remains within these limitations regular C++ can be used in constexpr function. However, when exploring/exploiting the compile-time computation features, one will recognize there are limitations and compile-time impacts can be really severe.

The tutorial covers many kinds of compile-time computations available in C++14, from constexpr functions and constants, literal types, variable templates, up to variadic templates using std::integer_sequence for string processing. It also gives a glimpse of the potential future standardized features, like user-defined-literal template operators for string literals. The tutorial also shows, where C++14 still has limitations in supporting compile-time computations and what you might need to do, to sidestep them.

Your Privacy

By clicking "Accept All Cookies" you agree ACCU can store cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

By clicking "Share IP Address" you agree ACCU can forward your IP address to third-party sites to enhance the information presented on the site, and that these sites may store cookies on your device.