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

烂漫一生 提交于 2019-11-29 09:07:54

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*.

It's an std::vector<something>. If you want a reference, you can do this:

auto & childs = node->getChilds();

That will of course be a const reference.

auto gives you std::vector<something>. You can either specify reference qualifier auto & or, alternatively, you can use decltype:

decltype( node->getChilds() ) childs = node->getChilds();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!