I'm not sure what the standard says about a case like this (mainly per
12.2/5):
class B {};
class D1: public B {};
class D2: public B {};
void foo( bool r )
{
B const& a = ( r ? D1() : D2() );
// Does the temporary live here?
}
itaj
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
itaj
|
6/27/2010 8:27:25 AM |
|
itaj sherman wrote:
> I'm not sure what the standard says about a case like this (mainly per
> 12.2/5):
>
> class B {};
> class D1: public B {};
> class D2: public B {};
>
> void foo( bool r )
> {
> B const& a = ( r ? D1() : D2() );
> // Does the temporary live here?
> }
>
First of all, that code isn't valid. Let's change it to
B const& a = ( r ? B() : D2() );
"Temporary" is a property of objects and strictly according to standards the
lifetime of it should be lengthened. But the aim of the Standard is that
only "direct" references to temporaries are lifetime-extended. What such a
"direct" reference is, however, isn't specified by the Standard as far as i
know.
In your example, even tho the result is an rvalue that definitely refers to
a temporary, that rvalue refers *either* a temporary coming from the left
*or* from the right side. Thus there is no direct reference anymore, in some
sense.
The issue around ?: and temporaries is subject of http://www.open-
std.org/jtc1/sc22/wg21/docs/cwg_active.html#462 . The direction that #462
drives toward is making "temporary" a property of expressions, instead of a
property of objects, without explicitly mentioning it in that issue report.
That issue report is the only place i know that would attribute it to
expressions explicitly. Many places in the Standard already seem to assume
that, while the text in the Standard is clear that it is a property of
objects.
To me it seems the following solves these questions too, with much less
overhead:
- Say that lifetime extension rules only apply to temporaries that haven't
been bound to references or to the implicit object parameter yet.
This is sort of what the RVO rules do, saying that they only apply to
temporaries that haven't yet been bound to references. I suspect here is a
reason they haven't done this way, but i can't think of a reason.
This would allow the above conditional-expression be lifetime extended too.
It still disallows extending lifetime of a "*this" refering to a temporary
object and other such things, which is the aim of the Standard. It would
keep the term "temporary" to strictly name a property of objects.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Johannes
|
6/28/2010 1:37:54 PM
|
|
itaj sherman wrote:
> I'm not sure what the standard says about a case like this (mainly per
> 12.2/5):
>
> class B {};
> class D1: public B {};
> class D2: public B {};
>
> void foo( bool r )
> {
> B const& a = ( r ? D1() : D2() );
> // Does the temporary live here?
> }
>
I'm sorry to annoy you again, but i've just noticed that the issue report
link i gave was discussing the comma operator instead of the conditional
operator (omg, epic fail). It turns out from links in that issue report
that the issue with the conditional operator has been fixed in C++0x by
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#446 . It
provides the "direct reference" i talked about.
Looking at it again, i found an interesting similarity with bit-fields: An
object can be a bit-field, but an expression can be a bit-field likewise.
Like, it's said "the result is of that type and value category and it is a
bit-field if ..." at 5.16/4 in the FCD. If it would omit the addendum, the
expression would potentially refer to a bit-field, but won't be a bit-field
itself. This seems to be exactly like the situation with temporaries. The
lifetime-lengthening subsection refers to the expression property, thus it
doesn't care about lvalues that refer to temporaries, because those lvalues
won't have the "temporary" property be true. But now, i'm just more confused
how these things fit together, really :(
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Johannes
|
6/28/2010 1:39:00 PM
|
|
On Jun 27, 5:27 pm, itaj sherman <itajsher...@gmail.com> wrote:
> I'm not sure what the standard says about a case like this (mainly per
> 12.2/5):
>
> class B {};
> class D1: public B {};
> class D2: public B {};
>
> void foo( bool r )
> {
> B const& a = ( r ? D1() : D2() );
> // Does the temporary live here?
>
> }
AFAIK, it does. E.g. ScopeGuard of Petru/Alexandrescu uses that
language feature.
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Goran
|
6/28/2010 1:39:59 PM
|
|
On 27 June, 17:27, itaj sherman <itajsher...@gmail.com> wrote:
> I'm not sure what the standard says about a case like this (mainly per
> 12.2/5):
>
> class B {};
> class D1: public B {};
> class D2: public B {};
>
> void foo( bool r )
> {
> B const& a = ( r ? D1() : D2() );
> // Does the temporary live here?
>
> }
Hi, the temporary in such a case lives as long as the reference does.
Have a look at this article:
http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/
Your example however will not compile, because operator ?: is not that
clever to deduce that you mean the common base type. You have to cast
at least one of the operands explicitly:
B const& a = r ? static_cast<B&>( D1() ): D2();
Regards,
&rzej
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
restor
|
6/28/2010 1:52:01 PM
|
|
On Jun 27, 6:27 pm, itaj sherman <itajsher...@gmail.com> wrote:
> I'm not sure what the standard says about a case like this (mainly per
> 12.2/5):
>
> class B {};
> class D1: public B {};
> class D2: public B {};
>
> void foo( bool r )
> {
> B const& a = ( r ? D1() : D2() );
//original code doesn't compile, it should have been:
B const& a = ( r ? static_cast<B const&>(D1()) : static_cast<B
const&>(D2()) );
//or:
B const& a = ( r ? B() : D2 );
> // Does the temporary live here?
//would that change the answer?
>
> }
>
> itaj
>
itaj
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
itaj
|
6/28/2010 6:29:16 PM
|
|
|
5 Replies
100 Views
(page loaded in 0.052 seconds)
Similiar Articles: Bad use of stringstream temporary? - comp.lang.c++Anything that makes this "keeping an eye on the lifetime" difficult ... So, your temporary is bound to this rvalue reference called 'os'. 'os' will be an lvalue expression ... Initialization of reference to non-const - comp.lang.c++.moderated ...Can anyone please explain to me the rational behind the decision that I cannot bind a reference to non-const to a temporary? void bar_01(Foo& f) {... writing robust software? - comp.lang.c++.moderated... iterators that validate during runtime out-of-bound ... Standard C++ Library Extensions: a Tutorial and Reference ... does have an infinite leak and one where the the life ... Reverse engineering ASIC into FPGA - comp.arch.fpga... some foreign language, crazy!! and nothing for reference ... give up essential liberty to obtain a little temporary ... Notes: [1] Term of joint works is measured by life ... problem with mixed c and fortran code - comp.lang.fortran ...... ALIAS:'_f_' :: f (I was getting unresolved reference to ... In call to I/O Write routine, an array temporary was ... Life is way simpler in 64-bit Windows where there is ... fmincon vs lsqnonlin - comp.soft-sys.matlabThe minimum will obviously be at x=0, in this case. Now loosen the lower bound to lb=-1. The new minimum will be at x=-1. These are the facts of life with constrained ... Odd BACKUP error: unsupported file structure ! - comp.os.vms ...... the disk (which is why I am doing an /IMAGE to a temporary ... Dev Prot S:RWPL,O:RWPL,G:R,W > Reference ... xserve, mount them with LD, and then create a bound ... ntpd, boot time, and hot plugging - comp.protocols.time.ntp ...... give up essential Liberty, to purchase a little temporary ... believe that using a VME-based GPS source as a reference ... seconds sounds like a reasonable upper bound) set the ... segmentation fault (SIGSEGV) - comp.lang.fortran... is as if the actual argument is assigned to a temporary ... arguments in the language, which would be bound to ... to a process when it makes an invalid memory reference, or ... problem with synchronizing two comps to each other - comp ...Even if you have a good external reference and the master ... in disconnected mode is going to make your life much ... give up essential Liberty, to purchase a little temporary ... object lifetime - Should this C++ temporary binding to reference ...You can see that in the case of const oogie& ref the immediately bound-to-reference temporary return value of func() has the extended lifetime of said reference (until end ... Const Reference To Temporary Is Useless « ASKLDJDIn this example, the temporary i’s lifetime is extended across its ... This reference is bound to the temporary or to a subobject with the temporary. 7/24/2012 1:06:34 PM
|