Note that there are some explanatory texts on larger screens.

plurals
  1. POmove constructor with unordered_map
    text
    copied!<p>I have some code:</p> <pre><code>Class A{ //...A has a move ctor here. }; unordered_map&lt;int, A&gt; bla; A tmp; //operations on tmp bla.insert(make_pair&lt;int, A&gt;(1, move(tmp))); </code></pre> <p>I wanna call move constructor instead of copy ctor of class A. Is this code correct? I think so. The weird thing is it compiles and works for Ubuntu Precise (g++ show version of 4.6.3). But on CentOS, it failed to compile. The first few lines are:</p> <pre><code> In substitution of ‘template&lt;class _From1, class _To1&gt; static decltype ((__test_aux&lt;_To1&gt;(declval&lt;_From1&gt;()), std::__sfinae_types::__one())) std::__is_convertible_helper&lt;_From, _To, false&gt;::__test(int) [with _From1 = _From1; _To1 = _To1; _From = const A&amp;; _To = A] [with _From1 = const A&amp;; _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&lt;const A&amp;, A, false&gt;::value’ /gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:1263:12: required from ‘struct std::is_convertible&lt;const A&amp;, A&gt;’ /gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/type_traits:116:12: required from ‘struct std::__and_&lt;std::is_convertible&lt;const int&amp;, int&gt;, std::is_convertible&lt;const A&amp;, A&gt; &gt;’ /gcc/x86_64-redhat-linux/4.7.1/../../../../include/c++/4.7.1/bits/stl_pair.h:113:38: required from ‘constexpr std::pair&lt;typename std::__decay_and_strip&lt;_T1&gt;::__type, typename std::__decay_and_strip&lt;_T2&gt;::__type&gt; std::make_pair(_T1&amp;&amp;, _T2&amp;&amp;) [with _T1 = int; _T2 = A; typename std::__decay_and_strip&lt;_T2&gt;::__type = A; typename std::__decay_and_strip&lt;_T1&gt;::__type = int]’ </code></pre> <p>It seems trying to call copy ctor. Any ideas on this error?</p> <p>Well, my CentOS does not have recent version of gcc/libstdc++, so actually i build these(gcc 4.7.1) by myself and install them in my home dir. Does this matter? This is my configuration when compiling:</p> <pre><code>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) </code></pre> <p><strong>UPDATE</strong>: Maybe there is sth wrong with the usage of "move semantic". I tried "move" with STL string:</p> <pre><code>unordered_map&lt;int, string&gt; bla; string tmp("hello world"); //operations on tmp bla.emplace(1, move(tmp)); </code></pre> <p>It is just ok and the internal chars are really "moved".</p> <p>But it does NOT work with my class A... This is A:</p> <pre><code>class A { public: A(){...} ~A(){} A(A&amp;&amp; other){...} private: A&amp; operator = (const A&amp; other); A&amp; operator = ( A&amp;&amp; other); A(const A&amp; other); }; </code></pre> <p><strong>UPDATE</strong>: i got it work, when A is:</p> <pre><code>class A { public: A(){...} ~A(){} A(A&amp;&amp; other){...} A(const A&amp; other){} private: A&amp; operator = (const A&amp; other); A&amp; operator = ( A&amp;&amp; other); }; </code></pre> <p>Pay attention to the <strong>COPY CTOR</strong>. Now all my move semantic works correct and <strong>copy ctor is not actually called</strong> when running. Do i get sth wrong about "move"?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload