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


Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 3 Jan 2010 12:07:27 CST
Raw View
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

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Sun, 3 Jan 2010 12:07:13 CST
Raw View
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.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Sean Hunt <rideau3@gmail.com>
Date: Sun, 3 Jan 2010 12:07:57 CST
Raw View
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


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: restor <akrzemi1@gmail.com>
Date: Sat, 2 Jan 2010 21:06:55 CST
Raw View
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-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]