为什么我们不能移动迭代器?

ano*_*ous 7 c++ move-semantics c++11

C++ 11引入了一种"移动"算法,其行为类似于"复制"算法,除了它......移动数据而不是复制它.我想知道为什么委员会没有更新复制算法以使用向前(或者可能除此之外).

向量提供了T&A的迭代器.常量const向量提供了const T的迭代器.有一个原因导致向量&&无法提供T &&的迭代器吗?这将允许使用vector的构造函数将元素从列表移动到向量...

这是个坏主意吗?

Ker*_* SB 16

我们已经有了.使用std::make_move_iterator创建移动迭代器.


How*_*ant 10

std::move_iterator.

#include <list>
#include <vector>

#include <iostream>

struct A
{
    A() = default;
    A(A&&) {std::cout << "move\n";}
    A(const A&) = default;
};

int main()
{
    std::list<A> l = {A(), A(), A()};
    std::vector<A> v(std::make_move_iterator(l.begin()),
                     std::make_move_iterator(l.end()));
}

move
move
move
Run Code Online (Sandbox Code Playgroud)