Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

are members of an r-value also r-values?

1 view
Skip to first unread message

restor

unread,
Jan 2, 2010, 10:06:55 PM1/2/10
to
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?

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 ]

Johannes Schaub (litb)

unread,
Jan 3, 2010, 1:07:27 PM1/3/10
to
restor wrote:

> 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

Mathias Gaunard

unread,
Jan 3, 2010, 1:07:13 PM1/3/10
to
On 3 jan, 03:06, restor <akrze...@gmail.com> wrote:
> 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 second is.
The first isn't because rValue().x is an rvalue.

Sean Hunt

unread,
Jan 3, 2010, 1:07:57 PM1/3/10
to
On Jan 2, 8:06 pm, restor <akrze...@gmail.com> wrote:
> 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 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

0 new messages