看看下面的代码:
#include <utility>
#include <map>
// non-copyable but movable
struct non_copyable {
non_copyable() = default;
non_copyable(non_copyable&&) = default;
non_copyable& operator=(non_copyable&&) = default;
// you shall not copy
non_copyable(const non_copyable&) = delete;
non_copyable& operator=(const non_copyable&) = delete;
};
int main() {
std::map<int, non_copyable> map;
//map.insert({ 1, non_copyable() }); < FAILS
map.insert(std::make_pair(1, non_copyable()));
// ^ same and works
}
编译此片段在取消对于g ++ 4.7标记行时失败。 产生的错误指示non_copyable
不能被复制,但我预期移动。
为什么插入std::pair
使用统一初始化失败,但不使用一个构造构造std::make_pair
? 两者不应该产生可成功移动到地图的右值?