标签:c-2 linux gcc c11 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)));
我想调用移动构造函数而不是复制类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.现在我的所有移动语义都正确,并且在运行时实际上并未调用复制ctor.我对“移动”感到错误吗?
解决方法:
不要为std :: make_pair指定模板参数,因为这会破坏其完美转发机制.即,如果你不能使用emplace,那么只需调用make_pair,如下所示:
bla.insert(make_pair(1, move(tmp)));
标签:c-2,linux,gcc,c11,unordered-map 来源: https://codeday.me/bug/20190620/1248807.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。