Const, Constexpr of C++ keywords

Const

Principle:
The compiler will replace all the places where the variable is used with the corresponding value during compilation.

const string name = "Mr.Chen";

Precautions:

  1. const declares a constant type.
  2. It can be read, but not modified.
  3. As long as the stored data is not changed, it is legal. As the following code
	const int num = 40;
	if (num)
	{
    
    
		/// <summary>
	}
	int numAdd = num + num;
  1. You cannot have a non-const type refer to a const type.
  2. Use of nonces:
    What is a nonce?
    When the compiler needs a space to temporarily store the job-seeking result of the expression, an unnamed object created temporarily is called a temporary object, or temporary for short.
    The following code:
       
    Code 1:
	int i = 10;
	const int& num = i;
	i = 1;
	cout << num << endl;

Code 2:

	double d = 3.14;
	const int& i = d;
	d = 4.14;
	cout << i << endl;

Code 1 outputs 1, and Code 2 outputs 3;
the reason is that for Code 1, the const int& is bound to an ordinary int object;
for the line of code const int& i = d; in Code 2, the right The type is converted to the left type, and the compiler will program Code 2 as follows:

	const int temp = d;//由双精度浮点数生成一个临时的整形常量
	const int& i = temp;//让i绑定这个临时量

So our modification of d will not affect the temporary quantity, nor will it affect the i bound to the temporary quantity

constexpr

What is a constant expression?
A constant expression refers to an expression whose value does not change and whose calculation result can be obtained during compilation . Whether an object (expression) is a constant expression is determined by its data type and initial value .
example:

	const int max_files = 20;	//max_files是常量表达式
	const int limit = max_files + 1;	// limit是常量表达式
	int staff_size = 27;	// staff_size不是常量表达式     数据类型只是一个普通的int类型而不是const int
	const int sx = get_size();	// sx不是常量表达式    get_size()具体值只有到运行时才可以获取到

constexpr variable:

  1. A variable declared as constexpr type must be a constant and must be initialized with a constant expression.
  2. The constant of constexpr type gets the initial value constant at compile time, because the constant expression must be used for initialization, and the constant expression gets the calculation result during compilation.
  3. Ordinary functions cannot be used as the initial value of constexpr variables. The new C++11 standard stipulates that functions modified with constexpr are allowed to be used as the initial value of constexpr variables. Such functions should be simple enough to calculate their results at compile time, but constexpr The return value of the decorated function is not necessarily a compile-time constant.
constexpr int foo(int i)
{
    
    
    return ++i;
}

int main()
{
    
    
    int i = 10;
    // 成功调用
    array<int, foo(5)> arr;
    // 成功调用
    foo(i);
    // 错误
    array<int, foo(i)> arr1;
    return 0;
}

In the above code, the first and second executions are correct, and the third execution will report an error.

  • For the first time, foo(5) uses the constant expression 5, so the result can be obtained during compilation to determine the size of the array, so this statement is correct;
  • In the second time, foo(i) uses a variable, and the result can be obtained at runtime, so this call is correct;
  • In the third time, foo(i) uses a variable, and the result can only be obtained at runtime, but the declaration of array requires that its size must be determined at compile time and cannot be changed, so this code is wrong.
    Therefore, for a function modified by constexpr, if the parameters passed in can be calculated at compile time, then this function will generate a value at compile time. If the parameters passed in cannot be calculated at compile time, the function modified by constexpr is the same as a normal function.

constexpr pointer:

For a pointer defined in a constexpr declaration, then constexpr is only valid for the pointer and has nothing to do with the object pointed to by the pointer

	const int* p = NULL;	// p是一个指向整形常量的指针
	constexpr int* q = NULL;	// q是一个指向整形的常量指针

The reason is that constexpr puts the defined object as the top-level const .

What are top-level const and bottom-level const?
A pointer is an object itself, and it can point to another object . Therefore, whether the pointer itself is constant and whether the pointer is constant are two independent issues. The top-level const means that the pointer itself is a constant, and the bottom-level const means that the pointed object is a constant.

The difference between const and constexpr

The difference between const and constexpr

Reprint address: https://www.cnblogs.com/dn96/p/10118471.html

Guess you like

Origin blog.csdn.net/weixin_44081533/article/details/112788782
Recommended