24

I have a method that returns const A &.

If I want to use auto, what is the right way to do it. Is this OK?

const auto &items = someObject.someMethod();

I see some people do this:

auto &items = someObject.someMethod();

I am not sure which one to use, and what are the differences really...

Edit:

In this case, are these two equivalent?

auto items = someObject.someMethod();
auto &items = someObject.someMethod();
2
  • 2
    "When in doubt, always clarify your intent."
    – Morwenn
    May 29, 2013 at 10:02
  • Your edit is quite a different problem (to be answered with "NO" and some references to the automatic type deduction rules). May 29, 2013 at 10:33

3 Answers 3

17

Even though the two forms are equivalent in this case, I would choose the first form anyway, since it communicates better the fact that your piece of code does not need to modify the state of the object returned by someMethod().

So my advice is to go for this:

const auto &items = someObject.someMethod();
4
  • I edited my original post, so what happens in this case? Are those two cases the same? May 29, 2013 at 10:14
  • 9
    @user2381422: No, they are not. But please, keep in mind, that changing your questions or adding new questions 1 hour after people have responded to the old one is not a very appropriate way of using SO. As long there is a post that answers your original question, you should accept it. And if you have a new question, ask a new question :) - possibly linking it to the old question if the two are related.
    – Andy Prowl
    May 29, 2013 at 10:21
  • I wonder if const x = expression; could be added to C++ as a shortcut for const auto x = expression;
    – robson3.14
    May 29, 2013 at 13:32
  • 2
    @robson3.14 I guess that would make parsing C++ code even more insane that it already is. Jun 6, 2013 at 10:53
9

In your case, there is no difference. The type which auto represents will be deduced to const A in both cases, so the type of items will be const A&.

There is a difference in what the "self-documented semantics" of the code is. The const auto & clearly states "I want a reference to what is returned, and I will not modify it." Just auto & says simply "I want a reference to what is returned."

3
  • 3
    The const will be deduced if not explicitly present. May 29, 2013 at 9:32
  • @Potatoswatter someMethod returns const & A
    – Spook
    May 29, 2013 at 9:50
  • I edited my original post, so what happens in this case? Are those two cases the same? May 29, 2013 at 10:14
9

More explicitness is usually better, the exception being separation of concerns. Implementation details shouldn't be mentioned on the client side of an interface.

If the receiver isn't supposed to care how the object is being received, then perfect forwarding is the way to go.

auto && items = someObject.someMethod();

Allowing const to be deduced in auto and, for example, binding a temporary value to auto & would be a major code smell for me.

  • auto const & should mean read-only
  • auto & should mean read-write
  • auto && should mean catch all, perhaps adopting a return by value object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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