假设我们有一个具有以下接口(interface)的对象:
struct Node_t {
... const std::vector< something >& getChilds() const;
} node;
现在,我使用 auto
访问该属性像这样的变量:
auto childs = node->getChilds();
childs
的类型是什么? ?一个std::vector< something >
还是对其中一个的引用?
最佳答案
childs
的类型将是 std::vector<something>
.
auto
由与 template type deduction 相同的规则提供支持.此处选择的类型与为 template <typename T> f(T t);
选择的类型相同在像f(node->getChilds())
这样的电话中.
同样,auto&
会让你得到与 template <typename T> f(T& t);
挑选的相同类型, 和 auto&&
会让你得到与 template <typename T> f(T&& t);
挑选的相同类型.
这同样适用于所有其他组合,例如 auto const&
或 auto*
.
关于c++ - 来自 const std::vector<>&; 的自动对象或引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577920/