Re: Is this legal?
"Benoit Mathieu" <benoit.mathieu 2@NOSPAM.free.f r.invalid> wrote...[color=blue]
> Victor Bazarov wrote:[color=green]
> > So, binding a temporary _directly_ to a const reference is what causes
> > the temporary to live as long as the reference[/color]
>
> I'll check this in my Stroustrup tomorrow...
>[color=green]
> > however, preserving
> > that const reference by initialising a different const reference with
> > it is NOT OK, since the lifetime of the first reference could be shorter
> > than that of the different const reference. Did I understand the gist
> > of your example correctly?[/color]
>
> I would formulate the rule like this:
> ******
> When you build a reference with a temporary object (like
> Object & o = Object(...); )
> the lifetime of Object is the same as the lifetime of the
> reference o.[/color]
This is incorrect. You cannot bind a terporary to a non-const reference.
This is not allowed by the Standard and compliant compilers should complain.
[color=blue]
> *******
> Claudio Jolowicz has another phrase to express this rule, if
> you prefer... (18 Apr 2004 21:38:38 +0000 (UTC))[/color]
It has nothing to do with what I prefer. It's mandated by the Standard.
[color=blue]
> [...][/color]
"Benoit Mathieu" <benoit.mathieu 2@NOSPAM.free.f r.invalid> wrote...[color=blue]
> Victor Bazarov wrote:[color=green]
> > So, binding a temporary _directly_ to a const reference is what causes
> > the temporary to live as long as the reference[/color]
>
> I'll check this in my Stroustrup tomorrow...
>[color=green]
> > however, preserving
> > that const reference by initialising a different const reference with
> > it is NOT OK, since the lifetime of the first reference could be shorter
> > than that of the different const reference. Did I understand the gist
> > of your example correctly?[/color]
>
> I would formulate the rule like this:
> ******
> When you build a reference with a temporary object (like
> Object & o = Object(...); )
> the lifetime of Object is the same as the lifetime of the
> reference o.[/color]
This is incorrect. You cannot bind a terporary to a non-const reference.
This is not allowed by the Standard and compliant compilers should complain.
[color=blue]
> *******
> Claudio Jolowicz has another phrase to express this rule, if
> you prefer... (18 Apr 2004 21:38:38 +0000 (UTC))[/color]
It has nothing to do with what I prefer. It's mandated by the Standard.
[color=blue]
> [...][/color]
Comment