auto from const std::vector<&;对象还是引用? [英] auto from const std::vector<>&; object or reference?
问题描述
假设我们有一个具有以下接口的对象:
suppose we have an object with the following interface:
struct Node_t {
... const std::vector< something >& getChilds() const;
} node;
现在,我使用 auto
变量访问属性,如下所示:
Now, i access the property with an auto
variable like this:
auto childs = node->getChilds();
childs
的类型是什么?一个 std::vector<什么 >
或对一个的引用?
what is the type of childs
? a std::vector< something >
or a reference to one?
推荐答案
childs
的类型将为 std::vector
.
auto
由与 模板类型推导相同的规则提供支持.此处选择的类型与为 template
在类似 f(node->getChilds())
的调用中.
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())
.
类似地,auto&
会为您提供与 template
和 auto&&
将获得与 template
.
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);
.
这同样适用于所有其他组合,例如 auto const&
或 auto*
.
The same applies for all other combinations, like auto const&
or auto*
.
这篇关于auto from const std::vector<&;对象还是引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!