408

This is a little subjective I think; I'm not sure if the opinion will be unanimous (I've seen a lot of code snippets where references are returned).

According to a comment toward this question I just asked, regarding initializing references, returning a reference can be evil because, [as I understand] it makes it easier to miss deleting it, which can lead to memory leaks.

This worries me, as I have followed examples (unless I'm imagining things) and done this in a fair few places... Have I misunderstood? Is it evil? If so, just how evil?

I feel that because of my mixed bag of pointers and references, combined with the fact that I'm new to C++, and total confusion over what to use when, my applications must be memory leak hell...

Also, I understand that using smart/shared pointers is generally accepted as the best way to avoid memory leaks.

2
  • 1
    It is not evil if you are writing getter-like functions/methods.
    – John Z. Li
    Feb 10, 2018 at 8:32
  • 1
    No, it is not. The user could always say whether he wants the reference (use &) or copy the reference (no &) so it is up to the user. It is in no way evil at all; but actually pretty convenient cause it gives more control and freedom to the developers, and also save memory.
    – Beyondo
    Jul 28, 2022 at 22:54

16 Answers 16

497

In general, returning a reference is perfectly normal and happens all the time.

If you mean:

int& getInt() {
    int i;
    return i;  // DON'T DO THIS.
}

That is all sorts of evil. The stack-allocated i will go away and you are referring to nothing. This is also evil:

int& getInt() {
    int* i = new int;
    return *i;  // DON'T DO THIS.
}

Because now the client has to eventually do the strange:

int& myInt = getInt(); // note the &, we cannot lose this reference!
delete &myInt;         // must delete...totally weird and  evil

int oops = getInt(); 
delete &oops; // undefined behavior, we're wrongly deleting a copy, not the original

Note that rvalue references are still just references, so all the evil applications remain the same.

If you want to allocate something that lives beyond the scope of the function, use a smart pointer (or in general, a container):

std::unique_ptr<int> getInt() {
    return std::make_unique<int>(0);
}

And now the client stores a smart pointer:

std::unique_ptr<int> x = getInt();

References are also okay for accessing things where you know the lifetime is being kept open on a higher-level, e.g.:

struct immutableint {
    immutableint(int i) : i_(i) {}

    const int& get() const { return i_; }
private:
    int i_;
};

Here we know it's okay to return a reference to i_ because whatever is calling us manages the lifetime of the class instance, so i_ will live at least that long.

And of course, there's nothing wrong with just:

int getInt() {
   return 0;
}

If the lifetime should be left up to the caller, and you're just computing the value.

Summary: it's okay to return a reference if the lifetime of the object won't end after the call.

11
  • 30
    These are all bad examples. The best example of proper usage is when you are returning a reference to an object that was passed in. ala operator <<
    – Arelius
    Jun 10, 2011 at 21:46
  • 194
    For the sake of posterity, and for any newer programmers chancing upon this, pointers are not bad. Neither are pointers to dynamic memory bad. They both have their legitimate places in C++. Smart pointers should definitely be your default go-to when it comes to dynamic memory management though, but your default smart pointer should be unique_ptr, not shared_ptr.
    – Jamin Grey
    Mar 6, 2013 at 20:22
  • 16
    Edit approvers: don't approve edits if you cannot vouch for its correctness. I've rollbacked the incorrect edit.
    – GManNickG
    Mar 20, 2014 at 4:01
  • 9
    For the sake of posterity, and for any newer programmers chancing upon this, do not write return new int. Apr 9, 2015 at 19:57
  • 8
    For the sake of posterity, and for any newer programmers chancing upon this, just return the T from the function. RVO will take care of everything.
    – Shoe
    Apr 9, 2015 at 19:59
67

No.

What is evil is making a reference to a dynamically allocated object and losing the original pointer. When you new an object you assume an obligation to have a guaranteed delete.

But have a look at, eg, operator<<: that must return a reference, or

cout << "foo" << "bar" << "bletch" << endl ;

won't work.

3
  • 31
    I downvoted because this neither answers the question (in which OP has made clear he knows the need for deletion) nor addresses the legitimate fear that returning a reference to a freestore object can lead to confusion. Sigh.
    – Iraimbilanja
    Apr 15, 2009 at 16:58
  • 5
    The practice of returning a reference object is not evil. Ergo no. The fear he expresses is a correct fear, as I point out in the second graf. Apr 15, 2009 at 17:01
  • 3
    Iraimbilanja@ About the "No"-s I don't care. but this post pointed out an important info which was missing from GMan.
    – Kobor42
    May 25, 2013 at 20:17
59

You should return a reference to an existing object that isn't going away immediately, and where you don't intend any transfer of ownership.

Never return a reference to a local variable or some such, because it won't be there to be referenced.

You can return a reference to something independent of the function, which you don't expect the calling function to take the responsibility for deleting. This is the case for the typical operator[] function.

If you are creating something, you should return either a value or a pointer (regular or smart). You can return a value freely, since it's going into a variable or expression in the calling function. Never return a pointer to a local variable, since it will go away.

6
  • 1
    Excellent answer but for "You can return a temporary as a const reference." The following code will compile but probably crash because the temporary is destroyed at the end of the return statement: "int const& f() { return 42; } void main() { int const& r = f(); ++r; }" Apr 16, 2009 at 11:36
  • @j_random_hacker: C++ has some strange rules for references to temporaries, where the temporary lifetime might be extended. I'm sorry I don't understand it well enough to know if it covers your case. Apr 16, 2009 at 16:41
  • 3
    @Mark: Yes, it does have some strange rules. A temporary's lifetime can only be extended by initialising a const reference (that is not a class member) with it; it then lives until the ref goes out of scope. Sadly, returning a const ref is not covered. Returning a temp by value is safe however. Apr 17, 2009 at 3:56
  • See the C++ Standard, 12.2, paragraph 5. Also see Herb Sutter's stray Guru of the Week at herbsutter.wordpress.com/2008/01/01/…. Apr 17, 2009 at 13:27
  • 4
    @David: When the function's return type is "T const&", what actually happens is that the return statement implicitly converts the temp, which is of type T, to type "T const&" as per 6.6.3.2 (a legal conversion but one which does not extend lifetime), and then the calling code initialises the ref of type "T const&" with the function's result, also of type "T const&" -- again, a legal but non-lifetime-extending process. End result: no extension of lifetime, and much confusion. :( Apr 18, 2009 at 16:28
46

I find the answers not satisfactory so I'll add my two cents.

Let's analyze the following cases:

Erroneous usage

int& getInt()
{
    int x = 4;
    return x;
}

This is obviously error

int& x = getInt(); // will refer to garbage

Usage with static variables

int& getInt()
{
   static int x = 4;
   return x;
}

This is right, because static variables are existant throughout lifetime of a program.

int& x = getInt(); // valid reference, x = 4

This is also quite common when implementing Singleton pattern

class Singleton
{
    public:
        static Singleton& instance()
        {
            static Singleton instance;
            return instance;
        };

        void printHello()
        {
             printf("Hello");
        };

};

Usage:

 Singleton& my_sing = Singleton::instance(); // Valid Singleton instance
 my_sing.printHello();  // "Hello"
    

Operators

Standard library containers depend heavily upon usage of operators which return reference, for example

T & operator*();

may be used in the following

std::vector<int> x = {1, 2, 3}; // create vector with 3 elements
std::vector<int>::iterator iter = x.begin(); // iterator points to first element (1)
*iter = 2; // modify first element, x = {2, 2, 3} now

Quick access to internal data

There are times when & may be used for quick access to internal data

Class Container
{
    private:
        std::vector<int> m_data;

    public:
        std::vector<int>& data()
        {
             return m_data;
        }
}

with usage:

Container cont;
cont.data().push_back(1); // appends element to std::vector<int>
cont.data()[0] // 1

HOWEVER, this may lead to pitfall such as this:

Container* cont = new Container;
std::vector<int>& cont_data = cont->data();
cont_data.push_back(1);
delete cont; // This is bad, because we still have a dangling reference to its internal data!
cont_data[0]; // dangling reference!
4
  • Returning the reference to a static variable can lead to undesired behavior e.g. consider a multiply operator which returns a reference to a static member then the following will always result to true: If((a*b) == (c*d))
    – clickMe
    Dec 21, 2016 at 20:55
  • Container::data()'s implementation should read return m_data;
    – Xeaz
    Feb 21, 2017 at 7:31
  • This was very helpful, thanks! @Xeaz wouldn't that cause issues with the append call though?
    – Andrew
    Feb 26, 2017 at 5:54
  • @Andrew No, it was a syntax shenanigan. If you, for example, returned a pointer type, then you would use reference address to create and return a pointer.
    – thorhunter
    Feb 27, 2017 at 10:23
16

It's not evil. Like many things in C++, it's good if used correctly, but there are many pitfalls you should be aware of when using it (like returning a reference to a local variable).

There are good things that can be achieved with it (like map[name] = "hello world")

4
  • 1
    I'm just curious, what is good about map[name] = "hello world"? Oct 20, 2011 at 7:03
  • 7
    @wrongusername The syntax is intuitive. Ever tried to increment the count of a value stored in a HashMap<String,Integer> in Java? :P Oct 20, 2011 at 7:29
  • Haha, not yet, but looking at HashMap examples, it does look quite gnarly :D Oct 20, 2011 at 16:45
  • Problem I had with this: Function returns reference to an object in a container, but the calling function code assigned it to a local variable. Then modified some properties of the object. Problem: Original object in the container stayed untouched. The programmer so easily overlooks the & in the return value, and then you get really unexpected behaviours...
    – flohack
    Jun 10, 2014 at 16:28
11

"returning a reference is evil because, simply [as I understand] it makes it easier to miss deleting it"

Not true. Returning a reference does not imply ownership semantics. That is, just because you do this:

Value& v = thing->getTheValue();

...does not mean you now own the memory referred to by v;

However, this is horrible code:

int& getTheValue()
{
   return *new int;
}

If you are doing something like this because "you don't require a pointer on that instance" then: 1) just dereference the pointer if you need a reference, and 2) you will eventually need the pointer, because you have to match a new with a delete, and you need a pointer to call delete.

8

There are two cases:

  • const reference --good idea, sometimes, especially for heavy objects or proxy classes, compiler optimization

  • non-const reference --bad idea, sometimes, breaks encapsulations

Both share same issue -- can potentially point to destroyed object...

I would recommend using smart pointers for many situations where you require to return a reference/pointer.

Also, note the following:

There is a formal rule - the C++ Standard (section 13.3.3.1.4 if you are interested) states that a temporary can only be bound to a const reference - if you try to use a non-const reference the compiler must flag this as an error.

7
  • 1
    non-const ref doesnt necessarily break encapsulation. consider vector::operator[]
    – Iraimbilanja
    Apr 15, 2009 at 17:02
  • that's a very special case... that's why I said sometimes, though I should really claim MOST OF THE TIME :)
    – Sasha
    Apr 15, 2009 at 17:04
  • So, you're saying that the normal subscript operator implementation a necessary evil? I neither disagree or agree with this; as I am none the wiser. Apr 15, 2009 at 17:06
  • I don't say that, but it can be evil if misused :))) vector::at should be used whenever possible....
    – Sasha
    Apr 15, 2009 at 17:07
  • eh? vector::at also returns a nonconst ref.
    – Iraimbilanja
    Apr 15, 2009 at 17:18
5

Not only is it not evil, it is sometimes essential. For example, it would be impossible to implement the [] operator of std::vector without using a reference return value.

5
  • Ah, yes of course; I think this is why I started using it; as when I first implemented the subscript operator [] I realized the use of references. I'm lead to believe that this is the de facto. Apr 15, 2009 at 17:03
  • 1
    Oddly enough, you can implement operator[] for a container without using a reference... and std::vector<bool> does. (And creates a real mess in the process)
    – Ben Voigt
    Jul 31, 2017 at 19:36
  • @BenVoigt mmm, why a mess? Returning a proxy is also a valid scenario for containers with complex storage that does not map directly to the outer types (like ::std::vector<bool> you've mentioned). Oct 12, 2017 at 18:31
  • 1
    @Sergey.quixoticaxis.Ivanov: The mess is that using std::vector<T> in template code is broken, if T might be bool, because std::vector<bool> has very different behavior from other instantiations. It's useful, but it should have been given its own name and not a specialization of std::vector.
    – Ben Voigt
    Oct 12, 2017 at 18:36
  • @BenVoight I agree on the point about strange decision of making one specialization "really special" but I felt that your original comment implies that returning a proxy is odd in general. Oct 12, 2017 at 18:42
2

Addition to the accepted answer:

struct immutableint {
    immutableint(int i) : i_(i) {}

    const int& get() const { return i_; }
private:
    int i_;
};

I'd argue that this example is not okay and should be avoided if possible. Why? It is very easy to end-up with a dangling reference.

To illustrate the point with an example:

struct Foo
{
    Foo(int i = 42) : boo_(i) {}
    immutableint boo()
    {
        return boo_;
    }  
private:
    immutableint boo_;
};

entering the danger-zone:

Foo foo;
const int& dangling = foo.boo().get(); // dangling reference!
1

return reference is usually used in operator overloading in C++ for large Object, because returning a value need copy operation.(in perator overloading, we usually don't use pointer as return value)

But return reference may cause memory allocation problem. Because a reference to the result will be passed out of the function as a reference to the return value, the return value cannot be an automatic variable.

if you want use returning refernce, you may use a buffer of static object. for example

const max_tmp=5; 
Obj& get_tmp()
{
 static int buf=0;
 static Obj Buf[max_tmp];
  if(buf==max_tmp) buf=0;
  return Buf[buf++];
}
Obj& operator+(const Obj& o1, const Obj& o1)
{
 Obj& res=get_tmp();
 // +operation
  return res;
 }

in this way, you could use returning reference safely.

But you could always use pointer instead of reference for returning value in functiong.

0

i think using reference as the return value of the function is much more straight forward than using pointer as the return value of the function. Secondly It would be always safe to use static variable to which the return value refer to.

0

Best thing is to create object and pass it as reference/pointer parameter to a function which allocates this variable.

Allocating object in function and returning it as a reference or pointer (pointer is safer however) is bad idea because of freeing memory at the end of function block.

-1
    Class Set {
    int *ptr;
    int size;

    public: 
    Set(){
     size =0;
         }

     Set(int size) {
      this->size = size;
      ptr = new int [size];
     }

    int& getPtr(int i) {
     return ptr[i];  // bad practice 
     }
  };

getPtr function can access dynamic memory after deletion or even a null object. Which can cause Bad Access Exceptions. Instead getter and setter should be implemented and size verified before returning.

-2

Function as lvalue (aka, returning of non-const references) should be removed from C++. It's terribly unintuitive. Scott Meyers wanted a min() with this behavior.

min(a,b) = 0;  // What???

which isn't really an improvement on

setmin (a, b, 0);

The latter even makes more sense.

I realize that function as lvalue is important for C++ style streams, but it's worth pointing out that C++ style streams are terrible. I'm not the only one that thinks this... as I recall Alexandrescu had a large article on how to do better, and I believe boost has also tried to create a better type safe I/O method.

3
  • 2
    Sure it's dangerous, and there should be better compiler error checking, but without it some useful constructs could not be done, e.g. operator[]() in std::map. Apr 16, 2009 at 11:38
  • 2
    Returning non-const references is actually incredibly useful. vector::operator[] for example. Would you rather write v.setAt(i, x) or v[i] = x? The latter is FAR superior.
    – mrr
    Oct 29, 2014 at 4:26
  • 1
    @MilesRout I'd go for v.setAt(i, x) any time. It is FAR superior.
    – scravy
    Aug 1, 2018 at 10:56
-3

I ran into a real problem where it was indeed evil. Essentially a developer returned a reference to an object in a vector. That was Bad!!!

The full details I wrote about in Janurary: http://developer-resource.blogspot.com/2009/01/pros-and-cons-of-returing-references.html

2
  • 2
    If you need to modify the original value in the calling code, then you need to return a ref. And that is in fact no more and no less dangerous than returning an iterator to a vector -- both are invalidated if elements are added to or removed from the vector. Apr 16, 2009 at 11:43
  • 1
    That particular problem was caused by holding a reference to a vector element, and then modifying that vector in a way which invalidates the reference: Page 153, section 6.2 of "C++ Standard Library: A Tutorial and Reference" - Josuttis, reads: "Inserting or removing elements invalidates references, pointers, and iterators that refer to the following elements. If an insertion causes reallocation, it invalidates all references, iterators, and pointers"
    – Trent Gm
    Feb 19, 2013 at 0:58
-16

About horrible code:

int& getTheValue()
{
   return *new int;
}

So, indeed, memory pointer lost after return. But if you use shared_ptr like that:

int& getTheValue()
{
   std::shared_ptr<int> p(new int);
   return *p->get();
}

Memory not lost after return and will be freed after assignment.

2
  • 13
    It is lost because the shared pointer goes out of scope and frees the integer.
    – user142019
    Jul 23, 2013 at 6:17
  • the pointer is not lost, the address of the reference is the pointer.
    – dgsomerton
    Apr 24, 2016 at 3:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.