What is the importance of evaluating things at compile time?
int x = 10;
and
constexprint x = 10;
Both of these are going to give us an integer data type with the value of 10. But the second example will be evaluated at compile time. Maybe I don't understand what "compile time" is, aside from it being what happens when the program compiles, so I'm kind of curious here.
What exactly is the importance of using compile time functions, and variables, in this case, constexpr? Why should one care about it? Is it purely performance? If so, how?
It wouldn't have any effect on the end user when the product ships would it?
To use it as a case label in a switch statement (often enums are used for this purpose).
1 2 3 4 5 6 7 8
constexprint bad_num = 0;
switch (value)
{
case bad_num:
std::cout << "the value is bad\n"default:
std::cout << "the value is fine\n"
}
To use it in static_assert.
1 2
constexprint required_int_size = 4;
static_assert(sizeof(int) < required_int_size, "int is too small on your platform");
To be able to use it with constexpr if.
1 2 3 4 5 6
constexprbool debug_mode = true;
ifconstexpr (debug_mode)
{
// this code will be ignored if debug_mode is set to false
std::clog << "value of x is " << x << '\n';
}