Link to link is not allowed in the ISO C ++ 2003 standard, but why is it allowed by compilers?

In accordance with ISO C ++ 2003, section 8.3.2

"links are not allowed"

But I tried the following code in Visual C ++ and Ideone, and both compilers work successfully with this code.

Ideone GCC C ++ 4.3.2

int main() { int i=2; int &ref_i=i; int &ref_ref_i=ref_i; // should be an error according to c++ 2003 standard cout<<i<<endl<<ref_i<<endl<<ref_ref_i; return 0; } 

I really got confused by looking at this compiler behavior; can anyone explain this?

+6
source share
4 answers

You are not creating a link to the link in your code.

This is just another int& , i.e. both are int references

(TC shows an example of an illegal C ++ 03 link to a link)


In the standard section of C ++ 11 Β§ 8.3.2, this is clearly shown by example (the prohibition of links to links, of course, has not changed between C ++ 03 and C ++ 11, but the collapse link is new in C ++ 11):

If typedef-name (7.1.3, 14.1) or decltype (7.1.6.2) specifies the type TR, which is a reference to type T, an attempt to create the type "lvalue reference to cv TR" creates the type "lvalue reference to T", in while trying to create a type of "rvalue reference to cv TR" creates a type of TR.

 int i; typedef int& LRI; typedef int&& RRI; LRI& r1 = i; // r1 has the type int& const LRI& r2 = i; // r2 has the type int& const LRI&& r3 = i; // r3 has the type int& 
+14
source

Links to links are illegal in C ++ 03, but you do not create them.

The following code tries to create a link to a link:

 int main(){ int c = 0; typedef int & IREF; int & c1 = c; IREF & c2 = c1; int & & c3 = c1; } 

... and fails when g ++ is put into C ++ 03 mode:

 > g++-4.9 -std=c++03 -O2 -Wall -pedantic -pthread main.cpp main.cpp: In function 'int main()': main.cpp:5:12: error: cannot declare reference to 'IREF {aka int&}' IREF & c2 = c1; ^ main.cpp:6:13: error: cannot declare reference to 'int&', which is not a typedef or a template type argument int & & c3 = c1; ^ 

As noted in another answer, C ++ 11 added new link folding rules to make IREF & c2 = c1; correct, but not int & & c3 = c1; , int & is neither a typedef name nor a decltype specifier.

+2
source

This is not a link to a link; after declaring a link, using the name in the expression behaves like the name of the object itself.

5 Expressions 6. If the expression is initially of type "reference to T" (8.3.2, 8.5.3), the type is brought to "T" before any further analysis, expression denotes an object or function denoted by a reference, and expression denotes the value l.

So when you do this:

 int &ref_ref_i=ref_i; 

ref_ref is actually a reference to the original variable i .

If you tried to do something like this:

 int i = 6; int &ri = i; int &&ri = ri; 

You would get a compiler error, as well as the quote you are proposing.

Note. This answer applies only to C ++ 2003 and previous standards and does not take into account rvalue references.

+1
source

After you have done:

 int i=2; int &ref_i=i; 

then i and ref_i are alternate names for the same variable. It is not useful to say that one of them is a link, and one of them is not, because there is no possible program that can distinguish β€œwhat is”.

This code is essentially identical:

 int ref_i = 2; int &i = ref_i; 
0
source

Source: https://habr.com/ru/post/972912/


All Articles