Is there a back_inserter variant that takes advantage of move?

南楼画角 提交于 2019-12-05 16:34:24

std::back_inserter() is a convenience function template for creating an std::back_insert_iterator object.

The class std::back_insert_iterator has already operator= overloaded for taking rvalue reference types, i.e.:

back_insert_iterator<Container>& operator=(typename Container::value_type&& value);

Therefore, std::back_insert_iterator is already prepared to take advantage of move semantics.

If std::back_insert_iterator does nothing but calling push_back on the container it's being constructed with, the question can be answered looking at the std::vector::push_back overload set:

void push_back(const T& value);
void push_back(T&& value);

So, here we obviously have an overload for rvalue references. Wouldn't it be weird, if the corresponding std::back_insert_iterator copies its argument when invoked with an rvalue reference? And indeed, we again have a very similar overload set:

back_insert_iterator<Container>&
    operator=(typename Container::const_reference value);
back_insert_iterator<Container>&
    operator=(const typename Container::value_type& value);

with the additional comment that

1) Results in container->push_back(value)
2) Results in container->push_back(std::move(value))

Coming back to your original question

Is there a std::back_inserter really moving somehow?

Yes, the iterator this function creates does take advantage of rvalue reference arguments.

Is there already a variant of std::back_inserter that takes advantage of move/emplace?

No, but it can be implemented. See this answer.

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