Jump to content
  • Advertisement
Sign in to follow this  
Switch0025

Returning: const reference vs value

This topic is 4144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been reading "Effective C++" by Scott Meyers lately and a question has come up that I can't seem to find the answer to; so hopefully some of you guys can help me. Heres the question: Is it better to have a function return by value or to return by constant reference? From what I have found some people say it's better to return by constant reference because it's faster. While others say it's better to return by value because it's safer:
Quote:
Another time when returning a const reference may not a good idea is when you are returning a reference to member data of an object. Although returning a const reference prevents anyone from changing the data by using it, it means that you have to have persistent data to back the reference--it has to actually be a field of the object and not temporary data created in the function. Once you make the reference part of the interface to the class, then, you fix the implementation details. This can be frustrating if you later wish to change your class's private data so the result of the function is computed when the function is invoked rather than actually be stored in the class at all times.
from http://www.cprogramming.com/tutorial/const_correctness.html

Share this post


Link to post
Share on other sites
Advertisement
Generally you shouldn't be returning by value unless you specifically need to. Even then it would be better to have an out parameter in the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
Generally you shouldn't be returning by value unless you specifically need to. Even then it would be better to have an out parameter in the function.

Wouldn't the return-value optimization take care of this, which is even covered in that very same book?
Giving your compiler a "hint" by returning a newly constructed object rather than a temporary created one should prevent any overhead created by returning by value.

Share this post


Link to post
Share on other sites
Quote:
Is it better to have a function return by value or to return by constant reference?

That's half a question.
Define what the function is, member function or stand alone.
Returning scope? local/ member variable, constant or static ?

Share this post


Link to post
Share on other sites
Quote:
Original post by kiome
Quote:
Original post by Dave
Generally you shouldn't be returning by value unless you specifically need to. Even then it would be better to have an out parameter in the function.

Wouldn't the return-value optimization take care of this, which is even covered in that very same book?
Giving your compiler a "hint" by returning a newly constructed object rather than a temporary created one should prevent any overhead created by returning by value.


It has more to do with exception handling and memory allocation, than optimization.

When returning objects by value, there's lots of annoying corner cases where you can get weird results. I think they are covered in the same book.

As for what is better, you cannot return new object by reference. Returning a reference might be faster than value, but not always.

Share this post


Link to post
Share on other sites
Honestly, the answer is, "it depends". As you get through the book, you will find that he gets into the many cases where you should not be returning by reference (most of which boil down to returning a reference to something that goes out of scope).

Share this post


Link to post
Share on other sites
It depends on the type/size.
If it is a POD type that is no larger than the register size, then it is best to return by value, because it cannot possibly be faster by const reference. At best, the code generated using const reference in that case may end up doing the exact same thing as passing by value.

However if the type is larger than the register size, and is not a POD type, then definitely use a const reference.

Share this post


Link to post
Share on other sites
What I mean by return-value optimization shows this implementation on the Microsoft C++ 8.0 Compiler.
What kind of advantage would returning by const reference have if this optimization is being applied?

Share this post


Link to post
Share on other sites
Quote:
Original post by dmail
Quote:
Is it better to have a function return by value or to return by constant reference?

That's half a question.
Define what the function is, member function or stand alone.
Returning scope? local/ member variable, constant or static ?


I wanted to make the question as general as possible so I could get more "overall" answers but if I had to give an example I would say it's a member function returning a non-static, non-const data member.

Share this post


Link to post
Share on other sites
Quote:
Original post by Switch0025
Quote:
Original post by dmail
Quote:
Is it better to have a function return by value or to return by constant reference?

That's half a question.
Define what the function is, member function or stand alone.
Returning scope? local/ member variable, constant or static ?


I wanted to make the question as general as possible so I could get more "overall" answers but if I had to give an example I would say it's a member function returning a non-static, non-const data member.


Ok that's better :)
I have to say I know its only the wording but there is no such a thing as a constant reference(references are constant by default, so to speak), instead it is a reference to constant.
ie
int const& foo
not
int & const foo


This may sound a little picky but what could you say the following is? are they all the same?
int const * foo
int * const foo
const int * foo

Mean while back at the ranch I would return a reference to constant in this instance.
For reasons see item 30 and 21 in Effective C++

Quote:

In spite of the foregoing discussion, you may someday be faced with a situation in which, pressed to achieve performance constraints, you honestly need to write a member function that returns a reference or a pointer to a less-accessible member. At the same time, however, you won't want to sacrifice the access restrictions that private and protected afford you. In those cases, you can almost always achieve both goals by returning a pointer or a reference to a const object. For details, take a look at Item 21.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!