Skip to content

Compile-Time Programming Explained

Photo of Richard Thomson
Hosted By
Richard T.
Compile-Time Programming Explained

Details

One unique feature of the C++ programming language is the ability to perform arbitrary computation at compile-time. Such computation can be used to influence the generation of run-time code. Compile-time programming allows you to write algorithms that select optimum implementation variants when applied to specific types.

In this month's meeting, Richard Thomson will give us a gentle introduction to the benefits of compile-time programming with examples. We'll take a look at how the C++ language has evolved to make compile-time programming simpler and easier to understand.

Prior to C++11, compile-time programming was exclusively the domain of "template meta-programming" and required some fairly involved syntax and lengthy compile times.

C++11 (https://en.wikipedia.org/wiki/C%2B%2B11) introduced constexpr functions (http://en.cppreference.com/w/cpp/language/constexpr) and variadic templates (http://en.cppreference.com/w/cpp/language/parameter_pack) that simplified template meta-programming. Alternative algorithm selection was still performed through template meta-programming and the SFINAE (http://en.cppreference.com/w/cpp/language/sfinae) (substitution failure is not an error) mechanism.

C++14 (https://en.wikipedia.org/wiki/C%2B%2B14) relaxes many of the constraints on constexpr functions allowing a larger range of computation to be performed at compile-time with a simpler syntax than template meta-programming. In the standard library , , , , and gained constexpr functions to allow their values to be used at compile-time.

C++17 (https://en.wikipedia.org/wiki/C%2B%2B17) introduced if constexpr (http://en.cppreference.com/w/cpp/language/if) to select different code at compile-time, alleviating the need to use template meta-programming for alternative algorithm selection. This dramatically simplifies the implementation of algorithms that optimize for the types on which they operate. Lambda functions (http://en.cppreference.com/w/cpp/language/lambda) were extended so that they could be declared constexpr.

C++20 may bring us compile-time static reflection (https://www.meetingcpp.com/blog/items/reflections-on-the-reflection-proposals.html) on classes and metaclasses (https://herbsutter.com/2017/07/26/metaclasses-thoughts-on-generative-c/). This can extend compile-time programming to allow users to create their own library of class concepts. Such user libraries can expose ideas such as an interface or a property that are testable and distributable without requiring special compiler support. They are, in a sense, extending the compiler with C++ source code. At the same time they offer the ability to simplify many existing techniques while bringing better integration into the language, both in syntax and error diagnostics.

Food will be provided, so please RSVP so we have a proper head count.

Photo of Utah C++ Programmers group
Utah C++ Programmers
See more events