c++ - 来自 const std::vector<>&; 的自动对象或引用?

标签 c++ c++11 auto

假设我们有一个具有以下接口(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/

相关文章:

c++ - 使用 libconfig 库的 C++ 应用程序的 cmake 编译问题

c++ - 为什么 C++11 删除的函数会参与重载决议?

c++模板使用来自基类的声明

C++11 外部作用域变量声明为 auto

c++ - 在 Visual Studio MSVC 14.0 断言下使用 boost 程序选项失败

c++ - 从用户获取字符串中的多个单词的代码

c++ - 返回前后字母的程序

c++ - 返回指向函数静态数据的指针是否合适?

c++ - 由引用 : what am I doing wrong? 引起的意外复制构造

c++ - 调用表达式中 Callable 参数的完美转发目的?