21

suppose we have an object with the following interface:

struct Node_t {
 ... const std::vector< something >& getChilds() const;
 } node;

Now, i access the property with an auto variable like this:

auto childs = node->getChilds();

what is the type of childs? a std::vector< something > or a reference to one?

lurscher
  • 25,930
  • 29
  • 122
  • 185

3 Answers3

24

The type of childs will be std::vector<something>.

auto is powered by the same rules as template type deduction. The type picked here is the same that would get picked for template <typename T> f(T t); in a call like f(node->getChilds()).

Similarly, auto& would get you the same type that would get picked by template <typename T> f(T& t);, and auto&& would get you the same type that would get picked by template <typename T> f(T&& t);.

The same applies for all other combinations, like auto const& or auto*.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • what is auto&& ? reference to reference ? what does it mean ? – Hicham Jan 10 '12 at 02:48
  • 2
    `&&` denotes an rvalue reference. It's a C++11 feature, just like `auto` is. `auto&&` can become either an lvalue reference (i.e. a "normal" reference like `&`) or an rvalue reference. – R. Martinho Fernandes Jan 10 '12 at 02:49
  • 2
    Here's a question that explains it: http://stackoverflow.com/questions/5481539/what-does-t-mean-in-c0x – R. Martinho Fernandes Jan 10 '12 at 02:56
  • To clarify, you're saying that reference collapsing rules apply to `auto&&`? – ildjarn Jan 10 '12 at 15:29
  • Good explanation, but doesn't actually directly answer the question. – Karu Aug 23 '13 at 13:58
  • @R.MartinhoFernandes I think your answer is a bit like "the same as whether the Riesz criterion is true for any e larger than 1/4" to answer "is the Riemann hypothesis true or false?". It is useful but does not answer the question, unless you know whether the Riesz criterion is true for any e larger than 1/4 ;-) Still +1 because it taught me something useful, but without the answer by Benjamin I still wouldn't know the actual answer. :-) – Boris Dalstein May 05 '16 at 06:54
  • @Boris that's a poor analogy because whether the Riesz criterion holds as such is not information readily available, while the template type deduction rules are. The first hit on Google for "template type deduction" is quite satisfactory. – R. Martinho Fernandes May 05 '16 at 10:25
  • @R.MartinhoFernandes The analogy may not be perfect, but I still think it is relevant :) My point is that your answer, while well-written and providing useful info, only answers the question indirectly. As a friendly note, I believe your own expertise makes you overestimate the ability of the average C++ developer to deduce the actual answer (`std::vector`) from the cppreference page you link. I am personally not able to do so *confidently*, despite 8 years of C++ experience in the field of computer graphics. We're often blind to other people's non-fluency in our topic of expertise. – Boris Dalstein May 06 '16 at 23:13
  • @R.MartinhoFernandes (or more simply, my point is that prepending your answer by "The type of `childs` would in this case be `std::vector`. Indeed, ..." would make it perfect. Providing the answer + the explanation. Not just the explanation without the answer, like you did, or the answer without the explanation, like the other answerers did ;) ) – Boris Dalstein May 06 '16 at 23:24
  • 1
    @Boris Fair enough. In my defense my answer was the last one posted, so I'm guessing that when I did so I did to add to the others and didn't expect it to become the accepted one. – R. Martinho Fernandes May 07 '16 at 09:32
  • That makes sense :) Now yours is truly the superior answer ;) – Boris Dalstein May 07 '16 at 21:39
21

It's an std::vector<something>. If you want a reference, you can do this:

auto & childs = node->getChilds();

That will of course be a const reference.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
3

auto gives you std::vector<something>. You can either specify reference qualifier auto & or, alternatively, you can use decltype:

decltype( node->getChilds() ) childs = node->getChilds();
lapk
  • 3,838
  • 1
  • 23
  • 28