auto from const std::vector<&;对象还是引用? [英] auto from const std::vector<>&; object or reference?

查看:24
本文介绍了auto from const std::vector<&;对象还是引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个具有以下接口的对象:

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(T t); 在类似 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 选择的类型相同的类型.f(T& t);auto&& 将获得与 template 选择的相同类型.f(T&&t);.

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&lt;&amp;;对象还是引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆