简体   繁体   English

自动从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;

Now, i access the property with an auto variable like this: 现在,我使用如下所示的auto变量访问属性:

auto childs = node->getChilds();

what is the type of childs ? 什么类型的childs a std::vector< something > or a reference to one? 一个std::vector< something >或对一个的引用?

The type of childs will be std::vector<something> . childs的类型将为std::vector<something>

auto is powered by the same rules as template type deduction . auto由与模板类型推导相同的规则提供支持。 The type picked here is the same that would get picked for template <typename T> f(T t); 这里选择的类型与为template <typename T> f(T t);选择的类型相同template <typename T> f(T t); in a call like f(node->getChilds()) . 在像f(node->getChilds())这样的调用中。

Similarly, auto& would get you the same type that would get picked by template <typename T> f(T& t); 同样, auto&将为您提供与template <typename T> f(T& t);相同的类型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&&将为您提供与template <typename T> f(T&& t); .

The same applies for all other combinations, like auto const& or auto* . 对于所有其他组合,例如auto const&auto* ,同样适用。

It's an std::vector<something> . 这是一个std::vector<something> If you want a reference, you can do this: 如果需要参考,可以执行以下操作:

auto & childs = node->getChilds();

That will of course be a const reference. 当然,这将是一个const引用。

auto gives you std::vector<something> . auto为您提供std::vector<something> You can either specify reference qualifier auto & or, alternatively, you can use decltype : 您可以指定参考限定词auto & ,也可以使用decltype

decltype( node->getChilds() ) childs = node->getChilds();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;std :: vector的非常量引用的无效初始化 <Object*> &” - invalid initialization of non-const reference of type ‘std::vector<Object*>&’ 如何从 std::vector 返回 object 的引用<std::vector<> &gt; </std::vector<> - how to return a reference of an object from std::vector<std::vector<>> 返回对对象向量的const引用 - Returning a const reference to vector of an object const向量中元素的引用 - Reference of element from a const vector 错误:'std::vector 类型的非常量引用的无效初始化<int> &amp;' 来自 'std::vector 类型的右值<int> *'</int></int> - error: invalid initialization of non-const reference of type ‘std::vector<int>&’ from an rvalue of type ‘std::vector<int>*’ 是否 std::map 从其 const 引用创建 object 的新非 const 副本 - Is std::map creating a new non const copy of object from its const reference const auto&和auto&if引用对象之间的区别是const - Difference between const auto & and auto & if object of reference is const 无效初始化类型为&#39;std :: vector的非const引用 <double> &&#39;来自一个类型的左值 - invalid initialization of non-const reference of type ‘std::vector<double>&’ from an rvalue of type 如何返回const std :: vector <Object *const> ? - How-to return a const std::vector<Object *const>? 带有指向const对象的const指针的std :: vector无法编译 - std::vector with const pointer to const object does not compile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM