Topic: Is lifetime "transitive" across cascaded returns?


Author: "Liviu" <lmillea@strata-g.com>
Date: 1998/03/12
Raw View
I am not trying to re-start the old threads on the lifetime of temporary
referrents.
[ In that respect, I side with the interpretation that in..
    class A bar1();
    void foo1()    {    A &a1 = bar1(); /* ... */ }
..the temporary object referred to by a1 stays alive for the lifetime of
a1,
that is throughout foo1(). ]

My question is - in case of..

    class A;
    A bar1();

    A &bar2()
    { return bar1(); }

    void foo2()
    {
    A &a2 = bar2();
      // ...
    }

..how do you read the standard to determine what is the lifetime
of the temporary returned by bar1() to bar2()
and passed by bar2() as a reference back to foo2() ?


Regards,
Liviu


P.S. If you believe this is a just convoluted example abusing
the standard, you may read my other <casting returned objects>
in comp.lang.c++.moderated for the simple down-to-earth
problem which started it all for me.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Crane <ron@spam-not.srsys.com>
Date: 1998/03/13
Raw View
Liviu wrote:

> ...
> My question is - in case of..
>
>     class A;
>     A bar1();
>
>     A &bar2()
>     { return bar1(); }
>
>     void foo2()
>     {
>     A &a2 = bar2();
>       // ...
>     }
>
> ..how do you read the standard to determine what is the lifetime
> of the temporary returned by bar1() to bar2()
> and passed by bar2() as a reference back to foo2() ?

Your example seems semantically equivalent to this example:

    A & bar2 ()
    {
        A a;
        return a;
    }

in which a lives only until the end of its enclosing scope. Thus, both
programs return references to invalid objects. See generally Meyers,
_Effective C++_, 2nd ed. at 131-32.

Peace,

Ron
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/03/15
Raw View
In article <6e97c8$emd$1@Nntp1.mcs.net>,
  "Liviu" <lmillea@strata-g.com> wrote:

> My question is - in case of..
>
>     class A;
>     A bar1();
>
>     A &bar2()
>     { return bar1(); }
>
>     void foo2()
>     {
>     A &a2 = bar2();
>       // ...
>     }
>
> ..how do you read the standard to determine what is the lifetime
> of the temporary returned by bar1() to bar2()

First: the program is illegal, since it requires binding temporaries
to non-const references.  A temporary can only be bound to a const
reference.

Supposing all of the references const: see section 12.2 concerning
the lifetime of temporaries, in particular paragraph 5, from which
I cite: "A temporary bound to the returned value in a function return
statement persists until the function exits."  The temporary is
destructed on exiting bar1, before returning to bar2.

Any use of a2 in foo2 results in undefined behavior.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orientie objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]