In C ++, if a const object is initialized with a constant expression, we can use our const object wherever a constant expression is required.
const int x = 10;
int a[x] = {0};
For example, we can make a case statement in switch.
constexpr can be used with arrays.
constexpr is not a type.
The constexpr keyword can be used in conjunction with the auto keyword.
constexpr auto x = 10;
struct Data { // We can make a bit field element of struct.
int a:x;
};
If we initialize a const object with a constant expression, the expression generated by that const object is now a constant expression as well.
Constant Expression : An expression whose value can be calculated at compile time.
x*5-4 // This is a constant expression. For the compiler, there is no difference between typing this expression and typing 46 directly.
Initialize is mandatory. It can be used for reading purposes only. It cannot be changed. Up to this point, there is no difference between the "const" and "constexpr" keywords.
NOTE: We can use constexpr and const in the same declaration.
constexpr const int* p;
Normally, the return value of a function is obtained at runtime. But calls to constexpr functions will be obtained as a constant in compile time when certain conditions are met.
NOTE : Arguments sent to the parameter variable of the function in function calls or to all parameter variables if there is more than one parameter, if C.E the return value of the function will be calculated in compile time. !!!
constexpr int square (int a){
return a*a;
}
constexpr int a = 3;
constexpr int b = 5;
int arr[square(a*b+20)] = {0}; //This expression is equal to int arr[35] = {0};
In order for a function to be a constexpr function, the return value type of the function and the type of the function's parameters must be in the type category called "literal type".
The constexpr functions are implicitly inline functions.
None of the constexpr functions need to be called with a constant expression.It is not mandatory. If this happens, the computation will not be done at compile time. It will be treated like a normal function call. Therefore, where the constant expression is required, we will no longer be able to use this expression.
1 ) The types used in the parameters of the function and the type of the return value of the function must be literal type.
2 ) A local variable with static life time should not be used inside the function.
3 ) If the function is legal, when we call this function with a constant expression in compile time, the compiler calculates the return value of the function in compile time.
4 ) The compiler needs to see the code of the function, so constexpr functions will almost always be in the header files.
5 ) In order for the function we created to be a constexpr function, the definition of the function must be in the header file.Thus, whichever source file includes that header file will see the function definition.
Normally with Default Member Initialization, static data members with const and integral types can be initialized within the class. However, in order to do this, there must be both "const" and "integral types".
If we use static constexpr then it doesn't have to be an integral type to initialize it inside the class. As long as I initialize it with a constant expression, there is no problem.
class Myclass {
const static int sx = 15; // OK
constexpr static int sy = 15; // OK
const static double sd = 1.5; // ERROR
constexpr static double sd = 1.5; // OK
};