struct POD {
int x;
int y;
};
POD rValue();
int & lRef = rValue().x; // valid?
int && rRef = rValue().y; // valid?
Is any of the two initializations valid?
Regards,
&rzej
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
> Hi, I don't seam to be able to find what the draft says about the
> reference binding to members of r-values. In the following example:
>
> struct POD {
> int x;
> int y;
> };
>
> POD rValue();
>
> int & lRef = rValue().x; // valid?
> int && rRef = rValue().y; // valid?
>
> Is any of the two initializations valid?
>
The first is invalid, the second is valid. 5.2.5/4:
"If E2 is declared to have type "reference to T," then E1.E2 is an lvalue;
[...] Otherwise If E1 is an lvalue, then E1.E2 is an lvalue; otherwise, it
is an rvalue."
Notice that named rvalue references are lvalues, making it not important
whether an lvalue or rvalue reference is meant above. There is some concern
though about the above rule, see http://www.open-
std.org/jtc1/sc22/wg21/docs/cwg_active.html#240
The second is.
The first isn't because rValue().x is an rvalue.
The latter is. A member of an rvalue is an lvalue if it's type is an
lvalue reference or a static data member, otherwise, it will have the
same lvalue-ness as the outer class.
Sean