Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

returning a (const) reference to a temporary

 
 
AdlerSam
Guest
Posts: n/a
 
      02-22-2011
Hi,

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. Thus, where is the problem that justyfies
the warning?
 
Reply With Quote
 
 
 
 
domachine
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 9:26*am, AdlerSam <Christof.Warl...@siemens.com> wrote:
> Hi,
>
> 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. Thus, where is the problem that justyfies
> the warning?


Who told you, the thing with the lifetime? I think you misunderstood
something. The const does to a reference, is making it constant.
Comparable to constant pointers.

class Test
{
public:
void non_const_method()
{
// Do something like writing to a member-variable
// ...
}

void const_method() const
{
// Do something constant like reading a variable
// ...
}
}

int main()
{
const Test& ref = Test();

ref.non_const_method(); // This doesn't work
ref.const_method(); // Yes this does
}


This is the only thing the const keyword does. We don't have a garbage
collector in C++.

Best regards Dominik
 
Reply With Quote
 
 
 
 
domachine
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 9:55*am, domachine <dominik.burgdoer...@googlemail.com>
wrote:
> On Feb 22, 9:26*am, AdlerSam <Christof.Warl...@siemens.com> wrote:
>
> > Hi,

>
> > 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. Thus, where is the problem that justyfies
> > the warning?

>
> Who told you, the thing with the lifetime? I think you misunderstood
> something. The const does to a reference, is making it constant.
> Comparable to constant pointers.
>
> class Test
> {
> public:
> * * void non_const_method()
> * * {
> * * * * // Do something like writing to a member-variable
> * * * * // ...
> * * }
>
> * * void const_method() const
> * * {
> * * * * // Do something constant like reading a variable
> * * * * // ...
> * * }
>
> }
>
> int main()
> {
> * * const Test& ref = Test();
>
> * * ref.non_const_method(); *// This doesn't work
> * * ref.const_method(); *// Yes this does
>
> }
>
> This is the only thing the const keyword does. We don't have a garbage
> collector in C++.
>
> Best regards Dominik


Sorry there's a mistake in my main.

It should be:

int main()
{
Test test;
const Test& ref = test;

ref.non_const_method(); // This doesn't work
ref.const_method(); // Yes this does

}
 
Reply With Quote
 
Paul Brettschneider
Guest
Posts: n/a
 
      02-22-2011
AdlerSam wrote:

> Hi,
>
> 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. 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.
 
Reply With Quote
 
AdlerSam
Guest
Posts: n/a
 
      02-22-2011
On 22 Feb., 10:07, Paul Brettschneider <paul.brettschnei...@yahoo.fr>
wrote:
> AdlerSam wrote:
> > Hi,

>
> > 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. Thus, where is the problem that justyfies
> > the warning?

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


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

http://herbsutter.com/2008/01/01/got...portant-const/

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.)


 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      02-22-2011
On 22 Feb., 10:21, AdlerSam wrote:
> On 22 Feb., 10:07, Paul Brettschneider wrote:
> > AdlerSam wrote:
> > > 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. Thus, where is the problem that justyfies
> > > the warning?

>
> > This assumption is - of course - nonsense.

>
> Hm - Then where do I have mistaken Herb Sutters GotW #88
>
> http://herbsutter.com/2008/01/01/got...portant-const/
>
> 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.)


This is just a simplification of the C++ rules. It only applies to
cases like

string source();

void test() {
string const& x = source();
// x still refers to a valid string object
cout << x << endl;
}

However, returning references to function-local objects is never ok,
NEVER.

Cheers!
SG
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      02-22-2011
"AdlerSam" <> wrote in message
news:e4e20a44-c514-410b-8557-
> Hi,
>
> 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. Thus, where is the problem that justyfies
> the warning?


Where does the reference (the return value in this case) go out of scope?
 
Reply With Quote
 
AdlerSam
Guest
Posts: n/a
 
      02-22-2011
Ok, got it, thanks for your help. I just got confused by the quoted
article, making me think that references may behave "smarter" than
plain pointers in that they may prevent the destruction of local
variables until they themselves go out of scope.
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      02-22-2011
AdlerSam Wrote:

> On 22 Feb., 10:07, Paul Brettschneider
> <paul.brettschnei...@yahoo.fr> wrote:
> > AdlerSam wrote:
> > > Hi,

> >
> > > 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. 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
>
>

http://herbsutter.com/2008/01/01/got...ost-important-
const/
>
> 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 go wrong when you assume this lifetime extension is transitive.
The lifetime of a temporary is indeed extended, but only to the lifetime of
the *initial* reference it is bound to.
If you use that reference to initialise a second reference, then the
lifetime of the temporary is not further extended.

In your initial example, the temporary is bound to the reference being
returned, so the lifetime of the temporary is extended to the lifetime of
the return value.
As this creates a great risk of getting a dangling reference (for example,
when you have the code "const X& x = f();"), most compilers will warn you
when you try to return a reference to something that won't live long enough
to be useful in the caller.

Bart v Ingen Schenau

 
Reply With Quote
 
AdlerSam
Guest
Posts: n/a
 
      02-22-2011
> You go wrong when you assume this lifetime extension is transitive.

Thanks particularly for this one sentence: It just explains everything
and puts my "gut feeling" on when references extend the lifetime of a
temporary and when it doesn't back on solid grounds!
 
Reply With Quote
 
 
 
Reply

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?) anon.asdf@gmail.com 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



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57