What is the difference between the constant x and & xx?
#include <iostream> using namespace std; int main() { const int x = 2; const int &xx = 3; cout << x << endl; cout << xx << endl; return 0; } Why do you need & ?
What is the difference between the constant x and & xx?
#include <iostream> using namespace std; int main() { const int x = 2; const int &xx = 3; cout << x << endl; cout << xx << endl; return 0; } Why do you need & ?
Semantically, these two ads are different.
const int x = 2; const int &xx = 2; In the first case, a constant object is declared, which is initialized by an integer literal. It may not even be allocated memory if it does not need to access the memory of the object. The compiler can use the value of this constant object at compile time. For example,
const int x = 2; int a[x]; The size of the declared array is known at compile time.
As for this announcement
const int &xx = 2; then a temporary object is first created for the value of expression 2 , and then a reference is defined to this temporary object. Where this temporary object will be located in memory will be known at the stage of program execution. Note that the temporary object itself is not constant. It is simply the result of evaluating an expression consisting of one operand - an integer literal 2.
In this regard, there is a different reaction of compilers to the use of the xx link.
For example, the MS VC 2016 Community compiler does not compile the following code snippet.
const int &xx = 2; int a[xx]; Error message
Error C2131 expression not defined by constant 623
As I think, MS VC ++ 2016 Community in this case is guided by the following provision of the C ++ standard about constant expressions (5.20 Constant expression, p. No. 2)
- This is a type of non-volatile conforma
Please note that it is indicated that there should be a reference to a constant object, while the temporary expression that initializes the link of the above code fragment is not constant. Therefore, the compiler generates an error message.
The same code snippet is successfully compiled on www.ideone.com. This compiler seems to be based on another subclause of this clause of the C ++ standard.
- It means that it refers to a non-volatile type of literal type ;
This subclause says nothing about the constancy of a literal temporary object. It is enough that he had a literal type.
So if you delve into the C ++ standard, and also look at the behavior of the compilers, the differences will be obvious. :)
const int &xx = 3; is a reference to a constant value of type int . Frankly, telling what a link is in C ++ is long enough, so let me just limit myself to this brief answer by sending you to the relevant literature.
A link is like a pseudonym for a variable, an alternative name, so to speak. In your particular case, changing the value of 3 is simply not possible - it is a constant, therefore the reference to this value is valid only as a const int .
x ) just optimized and thrown away. If you are not going to do something interesting with it - like getting an address. In this case, a temporary variable will be created in memory, where this triple is supposed to be, for example. If not, then most likely there will be nothing in memory. Neither will space be allocated for x . - HarryDifference at machine code level:
gcc 6.x
// const int x = 2; mov DWORD PTR [rbp-4], 2 // const int &xx = 3; mov eax, 3 mov DWORD PTR [rbp-20], eax lea rax, [rbp-20] mov QWORD PTR [rbp-16], rax gcc 4.5.x
// const int &xx = 3; mov DWORD PTR [rbp-8], 3 lea rax, [rbp-8] mov QWORD PTR [rbp-16], rax Obviously, the second instruction [ const int &xx = 3; ] more expensive than the first. Your cap .
An array the size of x? You are welcome. Not a single assembler instruction - provided that we do not use it in the future.
An array the size of xx, provided that we do not use it. You are welcome.
// gcc 6.x - int arr[xx]; // комментарии излишне mov rax, QWORD PTR [rbp-16] mov eax, DWORD PTR [rax] cdqe sub rax, 1 mov QWORD PTR [rbp-24], rax mov rax, QWORD PTR [rbp-16] mov eax, DWORD PTR [rax] cdqe mov r8, rax mov r9d, 0 mov rax, QWORD PTR [rbp-16] mov eax, DWORD PTR [rax] cdqe mov rsi, rax mov edi, 0 mov rax, QWORD PTR [rbp-16] mov eax, DWORD PTR [rax] cdqe sal rax, 2 lea rdx, [rax+3] mov eax, 16 sub rax, 1 add rax, rdx mov edi, 16 mov edx, 0 div rdi imul rax, rax, 16 sub rsp, rax mov rax, rsp add rax, 3 shr rax, 2 sal rax, 2 mov QWORD PTR [rbp-32], rax Source: https://ru.stackoverflow.com/questions/604434/
All Articles