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?
auto
gives youstd::vector<something>
. You can either specify reference qualifierauto &
or, alternatively, you can usedecltype
:The type of
childs
will bestd::vector<something>
.auto
is powered by the same rules as template type deduction. The type picked here is the same that would get picked fortemplate <typename T> f(T t);
in a call likef(node->getChilds())
.Similarly,
auto&
would get you the same type that would get picked bytemplate <typename T> f(T& t);
, andauto&&
would get you the same type that would get picked bytemplate <typename T> f(T&& t);
.The same applies for all other combinations, like
auto const&
orauto*
.It's an
std::vector<something>
. If you want a reference, you can do this:That will of course be a const reference.