Is this short program legal C ++?

when solving the test at http://cppquiz.org I found this interesting piece of code:

#include <iostream> int f(int& a, int& b) { a = 3; b = 4; return a + b; } int main () { int a = 1; int b = 2; int c = f(a, a);// note a,a std::cout << a << b << c; } 

My question in this program is legal C ++ or is it not? I am worried about a strict pseudonym.

+4
source share
2 answers

You specify a strict alias β€” but a strict alias is associated with different types of aliases . It does not apply here.

There is no rule prohibiting this code. Its moral equivalent is the following code:

 int x = 42; int& y = x; int& z = x; 

Or, more importantly, its equivalent to having multiple child nodes refer to the same parent node in the data structure of the tree.

+7
source

Yes, it is legal.

I could officially prove this only by citing most of the standard C ++ text.

You pass two links, both of which refer to the same object, which is great. Then you assign new values ​​to this single object. Also great.

+1
source

All Articles