这段代码让我困惑:
struct foo {
int i;
foo(int j) : i(j) {}
foo(const foo &) = delete;
foo(foo &&) = delete;
foo &operator=(const foo&) = delete;
foo &operator=(foo&&) = delete;
};
bool operator<(const foo &f1, const foo &f2)
{
return f1.i < f2.i;
}
int main(int argc, char **argv)
{
std::map<foo,int> f;
std::map<foo,int> f2 = f; //error (as expected)
std::map<foo,int> f3 = std::move(f); //no error (why?)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因为我没有得到任何错误,所以在移动地图时似乎没有创建关键对象(甚至没有将其他关键对象移入其中).
为什么不?我可以根据C++ 11标准依赖这种行为吗?
更一般地说,复制/移动要求std::map对键和值类型以及在什么条件下起作用?