good code to return const reference to function local object?

G

George2

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
 
J

James Kanze

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

varung

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.



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:[email protected]
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
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top