I have a code:
Class A{
I want to call the constructor move instead of copy ctor of class A. Is this code correct? I think so. It is strange that it compiles and works for Ubuntu Precise (g ++ show 4.6.3 version). But on CentOS, he could not compile. The first few lines:
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]'
Seems to be trying to call copy ctor. Any ideas on this error?
Well, my CentOS does not have the latest version of gcc / libstdC ++, so I create them myself (gcc 4.7.1) and install them in my home directory. Does it matter? This is my compilation configuration:
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)
UPDATE : Perhaps this is not the case with the "move semantics". I tried to "move" the STL line:
unordered_map<int, string> bla; string tmp("hello world");
This is normal, and the internal characters are really "moved".
But it does NOT work with my class A ... This is A:
class A { public: A(){...} ~A(){} A(A&& other){...} private: A& operator = (const A& other); A& operator = ( A&& other); A(const A& other); };
UPDATE : I got it working when A:
class A { public: A(){...} ~A(){} A(A&& other){...} A(const A& other){} private: A& operator = (const A& other); A& operator = ( A&& other); };
Pay attention to CTOR COPY . Now all my semantic actions of moving are correct, and copying ctor is not actually called at startup. Am I really mistaken about "moving"?