constant
Constants are fixed values that do not change during program execution. These fixed values are also called literals. Constants can be any basic data type, including integer numbers, floating point numbers, characters, strings, and Booleans. A constant is just like a regular variable, except that its value cannot be changed after it is defined.
Different periods:
Compile-time constants Constants are defined differently at run time:
Symbolic constant (#define)
Constant variable (const)
Enumeration values (enum)
# define PI (3.1415926)
Const double PI = 3.1415926;
enum Color { red, green, blue };
const char * cc = “hello world”; // The.rodata segment of the program image
A constant expression defines an expression that can be evaluated at compile time. Such expressions can be used as untyped template arguments, array sizes, and other contexts where constant expressions are required.
However, a constant is not a constant expression. Only a constant initialized with a constant expression can be a constant expression. A constant initialized with a nonconstant expression is just a constant. A constant is not a constant expression if its initial value is not a constant expression.
Constexpr specifier
The CONSTExpr specifier declares that a function or variable can be evaluated at compile time. These variables and functions (given appropriate function arguments) can be used where compile-time constant expressions are required.
Constant initialization sets the initial value of a static variable to a compile-time constant.
Nonlocal variable
All non-local variables with a static storage period are initialized (unless delayed) before the execution of main as part of program startup. All non-local variables with thread-local storage are initialized as part of the start of the thread, in order prior to the start of execution of the thread function. For these two variables, initialization occurs at two distinct stages:
Static initialization
If allowed, constant initialization is done first (see list of conforming cases in constant initialization). Zero initialization is performed for all other non-local static and thread-local variables. Practice:
Constant initialization is usually done at compile time and the precomputed object representation is stored as part of the program image. If the compiler does not do this, it is also guaranteed that this initialization occurs before any dynamic initialization. Zero-initialized variables are placed in the.bss segment of the program image, which takes up no disk space and is filled with zero by the operating system when the program is loaded. example
#include <iostream> #include <array> struct S { static const int c; }; const int S::c = 5; Constexpr int d = 10 * S::c; constexpr int d = 10 * S::c; / / constant expressions: S: : c before it initializes the int main () {STD: : cout < < "d =" < < < < d '\ n'; std::array<int, S::c> a1; // OK: S::c const int e = 10; std::array<int, e> a2; // OK: e is a constant expression}Copy the code
Output:
d = 50 #include <iostream> #include <array> struct S { static const int c; }; constexpr int d = 10 * S::c; Const int S::c = 5; // Const int S::c = 5; / / constant initialization int main () {STD: : cout < < "d =" < < < < d '\ n'; std::array<int, S::c> a1; // OK: S::c is a constant} main.cpp:8:28: error: the value of 'S::c' is not usable in a constant expression 8 | constexpr int d = 10 * S::c; | ~~~^ main.cpp:5:22: note: 'S::c' was not initialized with a constant expression 5 | static const int c; | ^ #include <iostream> #include <array> struct S { static const int c; }; const int d = 10 * S::c; Const int S::c = 5; const int S::c = 5; / / constants initialization, that occurs first int main () {STD: : cout < < "d =" < < < < d '\ n'; std::array<int, S::c> a1; // OK: S::c is a constant expression // STD ::array<int, d> a2; // error: d is not a constant}Copy the code
Output:
d = 50
In the process of learning C++, I often do not want to learn because there is no information or no one’s guidance, so I specially prepared a group, you can get PDF books, tutorials and so on for everyone to use for free! Those who join the group have a chance to get physical books.
Copy the code