Hello again!
In this video, we are going to look at constant expressions.
A constant expression has a value which is evaluated at compile time, and which cannot be changed.
So obviously, literals would be constant expressions.
We can also have values which are computed entirely from literals. And we can have values which are computed
from other constant expressions.
So if you have a constant expression, it is basically some computation involving literals.
We can also have a variable, which is a constant expression. For this,
the variable has to be initialized from a constant expression, and the variable cannot be modified after
being initialized.
For example, 42 and 99 are literals, so they are constant expressions.
"i" and "j" are initialized from constant expressions, and they cannot be modified after initialization.
So "i" and "j" are constant expressions.
Then "i + j" is a computation involving only constant expressions.
So "i + j" is therefore a constant expression.
So this means you could use "i + j" as the dimension of an array because the dimension of an array must
be a constant expression.
If we make "i" and "j" non-const, then they are still initialized from constant expressions, but
they can now be modified after being initialized.
So "i" and "j" are not constant expressions. And this will give a compiler error. Unless you are using g++,
when you have to use the "-pedantic" option. Because g++ allows this as a non-standard extension.
So here is the code.
We have "i" and "j" initialized from 42 and 99, which are constant expressions.
"i" and "j" cannot be modified after initialization, so they are constant expressions as well.
And then "i" and "j" is a constant expression, and can be used for the dimension of the array.
And then, that compiles.
We just get a warning about the unused local variable.
If I make "i" and "j" non-const, then they are no longer constant expressions. Because they can
be modified after initialization.
And then "i + j" is not a constant expression.
And we get an error. "Did not evaluate to a constant".
So let's look a bit more closely at what it means, for something to be const.
The key word has been around, since the early days of C++.
A variable, which is const, cannot be modified after being initialized.
There are two ways to initialize a const variable.
We can use a constant expression. So we have something like this.
So i is actually a constant expression.
We can also initialize a const variable for a value which is known at runtime. Because const just means you
cannot change the value.
So we could, for example, read something from the keyboard, and then use that to initialize a const
variable.
If we are talking about compile-time programming, which is the subject of this section, then this
could be interesting, because the compiler will know the value of "i".
On the other hand, this is no use. Because compilers have no way of knowing what users are going to do when
the program runs.
So we have two separate concepts here.
And in C++11, we have a new keyword, for the constant expression concept.
So the constexpr keyword means that a variable is a constant expression.
This means that the variable is evaluated at compile time and cannot be modified.
So with the variable "i", which we declared as const before, we can now declare this as constexpr, because
"i" is a constant expression.
So what are the differences between being const and being constexpr? A variable which is const
cannot be modified.
And this is mainly used for function arguments.
We pass an argument by reference to const, or pointer to const, for efficiency and safety. And the compiler
will make sure that the function cannot modify the argument.
If we have a variable which is constexpr, this means the value is known at compile time, and cannot be
modified.
And we use that for computing the value of constants.
And this is useful, because we can improve performance by doing calculations at compile time. Instead
of when the program runs.
Okay, so that s it for this video.
I will see you next time.
But until then, keep coding!