这段代码让我很困惑:
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;
}
因为我在那里没有错误,所以在移动地图时似乎没有创建关键对象(甚至没有将另一个关键对象移动到其中)。
为什么不?我可以根据 C++11 标准依赖这种行为吗?
更一般地说,键和值类型std::map
的复制/移动要求是什么?在什么条件下?