Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning a (const) reference to a temporary

Thread Tools

returning a (const) reference to a temporary

James Kanze
Posts: n/a
On Feb 22, 9:21 am, AdlerSam <(E-Mail Removed)> wrote:
> On 22 Feb., 10:07, Paul Brettschneider <(E-Mail Removed)>
> wrote:
> > AdlerSam wrote:

> > > I wonder why the following two lines produce a warning:

> > > class X {};
> > > const X &f() {return X();}

> > > $ g++ -c ref.cpp
> > > ref.cpp: In function ‘const X& f()’:
> > > ref.cpp:2: warning: returning reference to temporary

> > > As far as I understand, a const reference _extends_ the
> > > lifetime of a temporary until the very last reference
> > > instance that refers to the temporary goes out of scope.

That would require full garbage collection, and then some, in
order to implement.

> > > Thus, where is the problem that justyfies the warning?

> > This assumption is - of course - nonsense. If you want to
> > manage lifetime of objects, you usually use smart pointers
> > or containers.

> > Hope that helps.

> Hm - Then where do I have mistaken Herb Sutters GotW #88


> To quote the important part:

> > Normally, a temporary object lasts only until the end of the
> > full expression in which it appears. However, C++
> > deliberately specifies that binding a temporary object to
> > a reference to const on the stack lengthens the lifetime of
> > the temporary to the lifetime of the reference itself, and
> > thus avoids what would otherwise be a common dangling
> > reference error. In the example above, the temporary
> > returned by f() lives until the closing curly brace. (Note
> > this only applies to stack-based references. It doesn’t work
> > for references that are members of objects.)

You seem to misunderstand two important points:

-- first, the lifetime of the temporary is extended only to the
end of the lifetime of the reference the temporary
initializes, not to any other references, and

-- second, return involves a copy, so the reference being
returned is not the reference the temporary initialized.

Herb has simply used a common, but misleading formulation of the
rule, which is better stated as "initializing a reference with
a temporary extends the lifetime of the temporary to that of the
reference". When returning a reference, you (formally, at
least) initialize a local temporary reference with the return
expression, then return a copy of that reference, with the local
temporary reference going out of scope (and thus triggering the
destruction of the temporary used to initialize it).

James Kanze
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) C++ 7 02-12-2008 10:58 AM
Returning a reference to a temporary marco_segurini C++ 1 01-10-2005 04:25 PM
returning copy of the temporary string object qazmlp C++ 3 03-07-2004 04:57 PM
Returning a reference to an existing C++ object as a reference JustMe Perl Misc 1 08-29-2003 07:02 AM
iterator / returning reference to local temporary Alexander Stippler C++ 2 07-04-2003 04:40 PM