簡體   English   中英

使用unordered_map移動構造函數

[英]move constructor with 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)));

我想調用move構造函數,而不是A類的復制ctor。此代碼正確嗎? 我認同。 奇怪的是它可以編譯並適用於Ubuntu Precise(g ++顯示版本4.6.3)。 但是在CentOS上,它無法編譯。 前幾行是:

 In substitution of ‘template<class _From1, class _To1> static decltype     ((__test_aux<_To1>(declval<_From1>()), std::__sfinae_types::__one()))     std::__is_convertible_helper<_From, _To, false>::__test(int) [with _From1 = _From1; _To1 = _To1; _From = const A&; _To = A] [with _From1 = const A&; _To1 = A]’:
/gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:1258:70:   required from ‘constexpr const bool std::__is_convertible_helper<const A&, A, false>::value’
/gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:1263:12:   required from ‘struct std::is_convertible<const A&, A>’
/gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:116:12:   required from ‘struct std::__and_<std::is_convertible<const int&, int>, std::is_convertible<const A&, A> >’
/gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/bits/stl_pair.h:113:38:   required from ‘constexpr std::pair<typename std::__decay_and_strip<_T1>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = A; typename std::__decay_and_strip<_T2>::__type = A; typename std::__decay_and_strip<_T1>::__type = int]’

似乎正在嘗試調用復制ctor。 對這個錯誤有任何想法嗎?

好吧,我的CentOS沒有最新版的gcc / libstdc ++,因此實際上我是自己構建這些(gcc 4.7.1)並將其安裝在我的主目錄中。 這有關系嗎? 這是我在編譯時的配置:

Target: x86_64-redhat-linux
Configured with: ../gcc-4.7.1/configure --prefix=/home/bla/usr/ --with-mpc=/home/bla/usr/ --with-mpfr=/home/bla/usr/ --with-gmp=/home/bla/usr/ --disable-multilib --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++ --enable-java-awt=gtk --disable-dssi --disable-plugin --with-cpu=generic --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.7.1 (GCC)

更新 :也許“移動語義”的用法有些錯誤。 我嘗試使用STL字符串“移動”:

unordered_map<int, string> bla;
string tmp("hello world");
//operations on tmp
bla.emplace(1, move(tmp));

沒關系,內部字符真的“移動了”。

但這對我的A類不起作用...這是A:

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

更新 :當A是時,我可以使用它:

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

注意COPY CTOR 現在我所有的移動語義工作都正確,並且在運行時實際上並未調用copy ctor 我對“移動”有誤解嗎?

不要std::make_pair指定模板參數,因為這會破壞其完善的轉發機制。 即,如果您不能使用emplace ,則只需按以下方式調用make_pair

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

我相信這個問題的答案與這個問題相同,並且可能是重復的問題。

但是,當使用帶有移動構造函數的noexcept關鍵字時,我仍然沒有成功編譯它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM