I’m reading C++ Primer, 5th Ed., where on p. 71 they first give this code example:
const int ci = 0, &cj = ci;
decltype(ci) x = 0;
decltype(cj) y = x;
decltype(cj) z; //error
Then they say:
It is worth noting that
decltypeis the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers.
What do they mean by this? I don’t get it. The y there refers to x. So what’s the catch?
I believe they’re trying to say that
decltype(cj)will not give you the type of the object thatcjrefers to (that is,const int) but will give you the type ofcjitself. Soywill beconst int&.The case to compare this to is when using the name of a reference in an expression. The standard says:
That is, when using the name of a reference in an expression, it is not the reference that is acted on but the object it refers to. This is what gives a reference type the functionality of an “alias”.