lundi 27 juin 2016

C++11: Why decltype((x)) gives the type of x type reference? [duplicate]

This question already has an answer here:

C++11 standard says,if decltype is applied on an expression "(x)",e.g. if x is "int" then decltype gives "int&".

I don't get "why" this rule, so I did an experiment

 int x = 1; 
 decltype((x)) r = x; 
 r = 3; 
 cout << x << endl; 

Yes, "x" became "3". My question is: does "(x)" count as an "expression"? If yes, then I have this program:

 decltype((1))i = x; 
 i = 4; 
 cout << x << endl; 

Here "1" is a int constant,so is "(1)“ also an expression? If yes, should "decltype((1))" also give "int&"? The results shows no, x is not changed to 4.

Why the 2 experiments gives me different result types, how can I judge if "(x)" is an expression?

Thanks.

Aucun commentaire:

Enregistrer un commentaire