auto from const std::vector<>&; object or reference?

前端 未结 3 1446
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 19:01

suppose we have an object with the following interface:

struct Node_t {
 ... const std::vector< something >& getChilds() const;
 } node;

3条回答
  •  粉色の甜心
    2020-12-18 19:24

    The type of childs will be std::vector.

    auto is powered by the same rules as template type deduction. The type picked here is the same that would get picked for template f(T t); in a call like f(node->getChilds()).

    Similarly, auto& would get you the same type that would get picked by template f(T& t);, and auto&& would get you the same type that would get picked by template f(T&& t);.

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

提交回复
热议问题