Topic: case of extended lifetime of temporary bound to reference


Author: brok@rubikon.pl (Bronek Kozicki)
Date: Thu, 26 Aug 2004 22:29:03 GMT
Raw View
Does clause 12.2/5 apply to const reference x below? Or in other words:
is behaviour of this program well defined?

#include <cstdio>

struct Y
{
     int x;
     Y(int i) : x(i) {}
};

int main()
{
   const int& x = Y(20).x;
   std::printf("%i\n", x);  // x is still vallid ?
}

Thanks


B.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Fri, 27 Aug 2004 02:40:56 GMT
Raw View
Bronek Kozicki wrote:
> Does clause 12.2/5 apply to const reference x below? Or in other words:
> is behaviour of this program well defined?
>
> #include <cstdio>
>
> struct Y
> {
>     int x;
>     Y(int i) : x(i) {}
> };
>
> int main()
> {
>   const int& x = Y(20).x;
>   std::printf("%i\n", x);  // x is still vallid ?
> }

 From the binding point of view, yes.  If the reference is bound to
a sub-object (and 'x' is a sub-object of the temporary Y(20)), the
temporary has to exist for the lifetime of the reference.

Can we actually pass references to functions with ellipsis?  I know
that lvalue-to-rvalue conversion is applied, is that what's happening?

V

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Fri, 27 Aug 2004 02:41:54 GMT
Raw View
Bronek Kozicki wrote:
> Does clause 12.2/5 apply to const reference x below? Or in other words:
> is behaviour of this program well defined?

In the intentions, yes. However, there is a misprint in 12.2/5 that
makes the understanding of the relevant sentence difficult, see here:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#443

Too bad the correction didn't make it into the final version.

Of course, the fact that the reference is const is not important in this
context, if that was your doubt.

Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]