Hello, I'm studying the C++ programming language and I'm in a chapter where thebook introduces the constexpr qualifier, to initialize a constexpr variable we need of a constant expression that can be evaluated at compile time, what does evaluated at compile time mean ? What's the difference between a variable and a constant for the compiler ?
1 2
int a = 10;
constexprint b = 15; // what's the difference ?
what happens to the variable knwon at compile when the programs run ? the variable a for example is simply created in memory but what about b ?
Thank you for your help I'm just a beginner
evaluated at compile time simply means that the compiler is able to evaluate solve for the value of an expression when converting it to machine instructions
A simple example would be that of the size of a static array, in such an arry the size can't change when the program is running hence a rule of thumb is to provide the size of such an arry as a const integral, a simple integral literal or a constant expression so that the compiler will be able to determine it size before the program runs.
For a dynamic array whose size is determined at runtime it won't require that it size be know at compile time
int ar [5]; //this size must be know when the compiler is translating the code to machine instructions
/// the size is an integral literal 5;
constexprint f ()/// this is a constexpr function compiler can evaluate this expression at compile
{ /// time since it will always return a constant value of 4;
return 2+2;
}
constexprint f2 () /// this function is an error it violates rules of a const exp function
{
int x; /// not that most constexpr function I av come across only contains a return
cout <<"enter a value "; cin>> x; /// statement
return x+2;
}
constexprint k=f ()//correct k is a constexpr intialized bu a constexpr function
constexprint funct (int x) /// this is a constexpr function compiler can evaluate it at compile time
{ ///providef x is a const or constexpr statement;
return x*x;
}
E.g. constexprint z= funct (6); // this is ok
int val;
cout <<" enter a value "; cin>> val;
constexprint z1=funct (val);/// this is an error val is not a const or constexpr statement
compiler can always resolve a constexpr statement at compile time ; note that a constexpr statement or variable is implicitly const and should be initialized on declaration
z=6;// this is an error z is a constexpr variable.
integral are all const
eg 5/// can be used in a cconstexpr statement 5 will always be 5; 5=2 this is an error;
Other variables can be subject to change hence cant be used in constexpr statements
E.g. int h=7; /// h can't be used to intialize a constexpr because h isn't a const variable
constexprint expr=h; /// ! Error h isn't a const variable
"Compile time" means "when you compile the program from source code. "Run time" is when you are actually running the program. In a constexpr, the compiler must be able to reduce the expression to a constant at compile time.
1 2
int a = 10;
constexprint b = 15; // what's the difference ?
a is a variable. You can assign a new value to it. b is a constant - basically a synonym for 15. You can't assign a new value to b any more than you can "assign" 22 to 10.
what happens to the variable known at compile when the programs run ?
A constexpr stays the same. That's the whole point.
@dhayden So a constexpr variable will be treated as a constant value at compile time, but because this does not happen with the variables that do not use constexpr? for example :
int a = 10; // this is not a constant expression
in this case I'm initializing a with constant expression, however, the variable is not a constant expression right? why ? .
the variable is not a constant expression right? why ?
Right. It is a variable because you defined it as one when you said it was an int. The main thing about the variable is that you can change its values:
1 2 3 4 5 6
int a=10;
int b = 20;
a = 82;
a = b-17;
a = a+b;
// etc.