Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > good code to return const reference to function local object?

Reply
Thread Tools

good code to return const reference to function local object?

 
 
George2
Guest
Posts: n/a
 
      01-23-2008
Hello everyone,


1. Returning non-const reference to function local object is not
correct. But is it correct to return const reference to function local
object?

2. If in (1), it is correct to return const reference to function
local object, the process is a new temporary object is created (based
on the function local object) and the const reference is binded to the
temporary object, and the life time of the temporary object is
extended to the life time of const reference?

Or no need to create such a temporary object, just let the const
reference binded to the function local object itself?


thanks in advance,
George
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      01-24-2008
On Jan 23, 12:26 pm, George2 <george4acade...@yahoo.com> wrote:

> 1. Returning non-const reference to function local object is not
> correct. But is it correct to return const reference to function local
> object?


No. What would that change?

> 2. If in (1), it is correct to return const reference to function
> local object, the process is a new temporary object is created (based
> on the function local object) and the const reference is binded to the
> temporary object, and the life time of the temporary object is
> extended to the life time of const reference?


No. To begin with, a temporary is never created when a
reference is initialized with an lvalue. And even if it were,
"A temporary bound to the [reference type] return value of a
function persists until the function exits".

> Or no need to create such a temporary object, just let the
> const reference binded to the function local object itself?


Const or not, the reference binds to the function local object,
which is destructed when the function exits.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
varung@gmail.com
Guest
Posts: n/a
 
      01-24-2008
According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378..entry
You CAN return a const reference to a function local object and it IS
kept alive
as long as the reference lives.

A copy is NOT created, you are passing a const reference to the actual
temporary in the
function.



On Jan 24, 12:12 am, James Kanze <james.ka...@gmail.com> wrote:
> On Jan 23, 12:26 pm, George2 <george4acade...@yahoo.com> wrote:
>
> > 1. Returning non-const reference to function local object is not
> > correct. But is it correct to return const reference to function local
> > object?

>
> No. What would that change?
>
> > 2. If in (1), it is correct to return const reference to function
> > local object, the process is a new temporary object is created (based
> > on the function local object) and the const reference is binded to the
> > temporary object, and the life time of the temporary object is
> > extended to the life time of const reference?

>
> No. To begin with, a temporary is never created when a
> reference is initialized with an lvalue. And even if it were,
> "A temporary bound to the [reference type] return value of a
> function persists until the function exits".
>
> > Or no need to create such a temporary object, just let the
> > const reference binded to the function local object itself?

>
> Const or not, the reference binds to the function local object,
> which is destructed when the function exits.
>
> --
> James Kanze (GABI Software) email:james.ka...@gmail.com
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-24-2008
* :
> According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry
> You CAN return a const reference to a function local object and it IS
> kept alive
> as long as the reference lives.
>
> A copy is NOT created, you are passing a const reference to the actual
> temporary in the
> function.


Don't top-post.

Your interpretation of that article is incorrect.


Cheers, & hth.,

- Alf


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Kira Yamato
Guest
Posts: n/a
 
      01-24-2008
On 2008-01-24 12:48:23 -0500, said:

> According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378
> .entry
> You CAN return a const reference to a function local object and it IS
> kept alive
> as long as the reference lives.


You're confusing C++ with java.

>
> [...]


--

-kira

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-25-2008
On Jan 24, 6:48 pm, var...@gmail.com wrote:
> According
> tohttp://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry
> You CAN return a const reference to a function local object
> and it IS kept alive as long as the reference lives.


According to the standard, you cannot. And I'd suggest you read
the article you site; there is no mention of any reference type
return values at all in it.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
What to return -- object, reference or const reference Arv C++ 15 03-07-2008 09:15 PM
return reference to local variable, good code? George2 C++ 15 12-17-2007 11:19 PM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const class reference return from function, what does const do? Jim Langston C++ 2 05-11-2006 09:49 AM



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