我尝试过这样的事情:
std::copy(std::make_move_iterator(s1.begin()), std::make_move_iterator(s1.end()),
std::make_move_iterator(s2.begin()));
Run Code Online (Sandbox Code Playgroud)
并得到这个错误:
error: using xvalue (rvalue reference) as lvalue
*__result = std::move(*__first);
Run Code Online (Sandbox Code Playgroud)
这让我感到困惑.如果你使用同样的事情发生std::move.GCC内部似乎使用了一个名为std::__copy_move_a移动而不是复制的函数.你使用std::copy或是否重要std::move?
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstring>
struct Test
{
typedef std::string::value_type value_type;
std::string data;
Test()
{
}
Test(const char* data)
: data(data)
{
}
~Test()
{
}
Test(const Test& other)
: data(other.data)
{
std::cout << "Copy constructor.\n";
}
Test& operator=(const Test& other)
{
data = other.data;
std::cout << "Copy assignment operator.\n";
return …Run Code Online (Sandbox Code Playgroud)