Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   returning a (const) reference to a temporary (http://www.velocityreviews.com/forums/t743990-returning-a-const-reference-to-a-temporary.html)

AdlerSam 02-22-2011 08:26 AM

returning a (const) reference to a temporary
 
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?

domachine 02-22-2011 08:55 AM

Re: returning a (const) reference to a temporary
 
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

domachine 02-22-2011 08:58 AM

Re: returning a (const) reference to a temporary
 
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

}

Paul Brettschneider 02-22-2011 09:07 AM

Re: returning a (const) reference to a temporary
 
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.

AdlerSam 02-22-2011 09:21 AM

Re: returning a (const) reference to a temporary
 
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.)



SG 02-22-2011 09:34 AM

Re: returning a (const) reference to a temporary
 
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

Fred Zwarts 02-22-2011 09:57 AM

Re: returning a (const) reference to a temporary
 
"AdlerSam" <Christof.Warlich@siemens.com> wrote in message
news:e4e20a44-c514-410b-8557-1dda58cbc787@x4g2000prf.googlegroups.com
> 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?

AdlerSam 02-22-2011 10:18 AM

Re: returning a (const) reference to a temporary
 
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.

Bart van Ingen Schenau 02-22-2011 11:25 AM

Re: returning a (const) reference to a temporary
 
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


AdlerSam 02-22-2011 12:28 PM

Re: returning a (const) reference to a temporary
 
> 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!


All times are GMT. The time now is 05:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.