January 2018
Intermediate to advanced
374 pages
9h 53m
English
To verify that constexpr is evaluated at compile time, you can use std::integral_constant. The integral constant is a template class that takes an integer type and an integer value as template parameters. From these, it generates a new class representing a number. If the compiler cannot evaluate the integer value at compile time, it won't compile. The value of the class is then accessed via the static field value of std::integral_constant.
Here is an example of compile-time integral values versus runtime integral values:
const auto ksum = std::integral_constant<int, sum(1,2,3)>;
auto func() -> void {
// This compiles as the value of sum is evaluated at compile time const auto sum_compile_time ...