Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning const reference to local variable from a function

I have some questions on returning a reference to a local variable from a function:

class A { public:     A(int xx)     : x(xx)     {         printf("A::A()\n");     } };  const A& getA1() {     A a(5);     return a; }  A& getA2() {     A a(5);     return a; }  A getA3() {     A a(5);     return a; }  int main() {     const A& newA1 = getA1(); //1     A& newA2 = getA2(); //2     A& newA3 = getA3(); //3 } 

My questions are =>

  1. Is the implementation of getA1() correct? I feel it is incorrect as it is returning the address of a local variable or temporary.

  2. Which of the statements in main (1,2,3) will lead to undefined behavior?

  3. In const A& newA1 = getA1(); does the standard guarantee that a temporary bound by a const reference will not be destroyed until the reference goes out of scope?

like image 346
aJ. Avatar asked Sep 23 '09 13:09

aJ.


People also ask

How do I return references to const?

You want to return a const reference when you return a property of an object, that you want not to be modified out-side of it. For example: when your object has a name, you can make following method const std::string& get_name(){ return name; }; . Which is most optimal way.

Can a function return const?

It's pretty pointless to return a const value from a function. it's questionable whether this is of any benefit to anyone. Returning a reference is different, but unless Object is some template parameter, you're not doing that. Note that the second example only triggers an error for built-in types.

Can we return a local variable by reference?

Objects returned by reference must live beyond the scope of the function returning the reference, or a dangling reference will result. Never return a local variable by reference.

Can we return reference of local variable in C++?

Returning values by reference in C++ A C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var.


2 Answers

1. Is getA1() implementation correct ? I feel it is incorrect as it is returning address of local variable or temporary.

The only version of getAx() that is correct in your program is getA3(). Both of the others have undefined behaviour no matter how you use them later.

2. Which of the statements in main ( 1,2,3) will lead to undefined behavior ?

In one sense none of them. For 1 and 2 the undefined behaviour is as a result of the bodies of the functions. For the last line, newA3 should be a compile error as you cannot bind a temporary to a non const reference.

3. In const A& newA1 = getA1(); does standard guarantees that temporary bound by a const reference will not be destroyed until the reference goes out of scope?

No. The following is an example of that:

A const & newConstA3 = getA3 (); 

Here, getA3() returns a temporary and the lifetime of that temporary is now bound to the object newConstA3. In other words the temporary will exist until newConstA3 goes out of scope.

like image 199
Richard Corden Avatar answered Sep 29 '22 08:09

Richard Corden


Q1: Yes, this is a problem, see answer to Q2.

Q2: 1 and 2 are undefined as they refer to local variables on the stack of getA1 and getA2. Those variables go out of scope and are no longer available and worse can be overwritten as the stack is constantly changing. getA3 works since a copy of the return value is created and returned to the caller.

Q3: No such guarantee exists to see answer to Q2.

like image 28
dharga Avatar answered Sep 29 '22 06:09

dharga