auto from const std :: vector<>& ;;对象还是引用? [英] auto from const std::vector<>&; object or reference?
问题描述
假设我们有一个具有以下接口的对象:
struct Node_t {
... const std: :vector&东西>& getChilds()const;
}节点;
现在,使用 auto
变量如下:
auto childs = node-> getChilds
childs
的类型是什么? a std :: vector< c>
将为 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<>& ;;对象还是引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!