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

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

问题描述

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

  struct Node_t {
... const std: :vector&东西>& getChilds()const;
}节点;

现在,使用 auto 变量如下:

  auto childs = node-> getChilds 

childs 的类型是什么? a std :: vector< c>

childs 将为 std :: vector< something>



auto 的功能与模板类型扣除。这里选择的类型与 template< typename T> f(node-> getChilds())


b $ b

类似地, auto& 会得到与 template>< typename T& f(T& t); auto&& 会得到与 template< typename T> f(T& t);



同样适用于所有其他组合,例如 auto const& auto *


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?

解决方案

The type of childs will be std::vector<something>.

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()).

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);.

The same applies for all other combinations, like auto const& or auto*.

这篇关于auto from const std :: vector&lt;&gt;&amp ;;对象还是引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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