Difference between revisions of "Constexpr"
(Created page with "<code>constexpr</code> is a keyword applied to variables and functions. It's basically the same as the <code>http://wiki.planetchili.net/index.php?title=Const const</code>...") |
m |
||
Line 20: | Line 20: | ||
</li></ol> | </li></ol> | ||
<source>const int y = GenRandomNumber();</source> | <source>const int y = GenRandomNumber();</source> | ||
− | In this case the value of y would be impossible to determine at compile time as a we don’t know what random number would be generated | + | In this case the value of y would be impossible to determine at compile time as a we don’t know what random number would be generated at runtime. |
NOTE: the variable is still a constant, once a random number is assigned to it the value cannot change. | NOTE: the variable is still a constant, once a random number is assigned to it the value cannot change. |
Revision as of 13:01, 2 September 2018
constexpr
is a keyword applied to variables and functions. It's basically the same as the [const]
keyword but enforces an additional rule that "The value of this const will be available at compile time"
Contents
Difference between const & constexpr
Basics
In C++ there can be 2 types of constants:
Whose value is known at compile time.
const int x = 50;
const int z = x + 1;
During compile time, the compiler knows the value of x and z would be 50 and 51 respectively. And the values won’t change throughout the lifetime of the program. So it goes “I’ll just substitute all the references to x and z with 50 and 51, instead of looking up what the values of x and z every time during runtime”.
This allows 50 and 51 to be used as literals (hardcoded values) with all their advantages and at the same time not clutter our code with random numbers that are hard to interpret the meaning of.
Whose value is not known at compile time.
const int y = GenRandomNumber();
In this case the value of y would be impossible to determine at compile time as a we don’t know what random number would be generated at runtime.
NOTE: the variable is still a constant, once a random number is assigned to it the value cannot change.
When the compiler comes across scenario #1 it marks x an z as constant expressions i.e. their value can be evaluated at compile time and in scenario #2 marks y as being a constant but not a constant expression as it can only be evaluated during runtime.
This can be a bit confusing in certain contexts e.g.
int anArray[x]; // Compiles successfully
int anotherArray[y]; // Fails compilation
Certain operations require compile-time knowledge of a variable, in this case, anotherArray declaration would throw an error even though y is a const just like x. This is because the value of y cannot be computed until the function GenRandomNum() is run.
Due to this confusion, it is hard to evaluate what constants will be available during compile time, and which won’t like in our example above. As your code base gets bigger this can spiral out of control, as multiple people are working on the same code base.
This is where the keyword constexpr
comes in as it tells the compiler that the value of this variable should be deduced at compile time to allow for scenarios such as the one above. If y was declared as constexpr int y = GetRandomNumber()
the compiler would throw an error that the value cannot be evaluated at compile time. It clarifies the intended use of the variable.