C++ 使用无序的_映射移动构造函数

C++ 使用无序的_映射移动构造函数,c++,linux,gcc,c++11,unordered-map,C++,Linux,Gcc,C++11,Unordered Map,我有一些代码: Class A{ //...A has a move ctor here. }; unordered_map<int, A> bla; A tmp; //operations on tmp bla.insert(make_pair<int, A>(1, move(tmp))); 更新: “移动语义”的用法可能有问题。我尝试用STL字符串“移动”: unordered_map<int, string> bla; string tmp("hel

我有一些代码:

Class A{
//...A has a move ctor here.
};

unordered_map<int, A> bla;
A tmp;
//operations on tmp
bla.insert(make_pair<int, A>(1, move(tmp)));
更新: “移动语义”的用法可能有问题。我尝试用STL字符串“移动”:

unordered_map<int, string> bla;
string tmp("hello world");
//operations on tmp
bla.emplace(1, move(tmp));
更新: 当A是:

class A
{
    public:
        A(){...}
        ~A(){}
        A(A&& other){...}
        A(const A& other){}
    private:
        A& operator = (const A& other);
        A& operator = ( A&& other);
};

请注意复制CTOR。现在,我所有的移动语义都正确无误,运行时,copy-ctor实际上没有被调用。“移动”是否有问题?

不要指定
std::make_pair
的模板参数,因为这会破坏其完美的转发机制。也就是说,如果您不能使用
emplace
,只需调用
make\u pair
,如下所示:

bla.insert(make_pair(1, move(tmp)));

我相信这个问题的答案与这个问题相同,也可能是一个重复的问题


但是,在使用带有move Constructor的noexcept关键字时,我仍然没有成功编译它。

在Arch Linux上使用g++4.7.1进行了测试,效果良好。实际上,如果libstdc++版本足够新,您应该使用
。emplace
<代码>bla.emplace(1,std::move(tmp))@AlexFarber我想是的。gcc似乎不承认“移动语义”。gcc-v显示了正确的版本,我会继续跟踪它。@KennyTM谢谢。我以后再试试。据我记忆所及,emplace直到最近才被实现?可变模板和右值引用随GCC4.3一起出现。因此,GCC 4.6.3中应包含
emplace
class A
{
    public:
        A(){...}
        ~A(){}
        A(A&& other){...}
    private:
        A& operator = (const A& other);
        A& operator = ( A&& other);
        A(const A& other);
};
class A
{
    public:
        A(){...}
        ~A(){}
        A(A&& other){...}
        A(const A& other){}
    private:
        A& operator = (const A& other);
        A& operator = ( A&& other);
};
bla.insert(make_pair(1, move(tmp)));