Boost C++ Libraries: Ticket #12307: Copy assignment from const ref handled differently in C++11/C++98 https://svn.boost.org/trac10/ticket/12307 <p> There is a difference in the handling of copy assignment when using Boost.Move between C++11 and C++98: </p> <pre class="wiki"> #include &lt;boost/move/move.hpp&gt; #include &lt;cassert&gt; class Foo{ BOOST_COPYABLE_AND_MOVABLE(Foo) public: int i; explicit Foo(int val): i(val){} Foo(BOOST_RV_REF(Foo) obj): i(obj.i) {} Foo&amp; operator=(BOOST_RV_REF(Foo) rhs){ i = rhs.i; rhs.i = 0; return *this; } Foo&amp; operator=(BOOST_COPY_ASSIGN_REF(Foo) rhs){ i = rhs.i; return *this; } //(1) template&lt;class OTHER&gt; Foo&amp; operator=(const OTHER&amp; rhs){ i = rhs.j; return *this; } //(2) }; struct Bar{ int j; explicit Bar(int val): j(val){} }; int main(){ Foo foo2(2); // Working cases Foo foo1(1); Bar bar(3); assert(foo1.i == 1); assert(foo2.i == 2); assert(bar.j == 3); foo2 = foo1; assert(foo1.i == 1); assert(foo2.i == 1); foo1 = bar; assert(foo1.i == 3); foo2 = boost::move(foo1); assert(foo1.i == 0); assert(foo2.i == 3); // BUG const Foo foo4(4); foo2 = foo4; // Calls (1) in C++11 but (2) in C++98 which fails assert(foo2.i == 4); assert(foo4.i == 4); return 0; } </pre><p> This is slighlty different than <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/12194"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/12194</a> which was fixed with <a class="ext-link" href="https://github.com/boostorg/move/pull/9"><span class="icon">​</span>https://github.com/boostorg/move/pull/9</a> but kind of releated. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12307 Trac 1.4.3 Ion Gaztañaga Thu, 30 Jun 2016 11:28:25 GMT <link>https://svn.boost.org/trac10/ticket/12307#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12307#comment:1</guid> <description> <p> There is no much the emulation can do in this case. It's a limitation that should be documented. </p> </description> <category>Ticket</category> </item> <item> <author>a.grund@…</author> <pubDate>Thu, 30 Jun 2016 11:43:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12307#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12307#comment:2</guid> <description> <p> I guessed that. But it is not documented and the current documentation statest that this should be possible: <a href="http://www.boost.org/doc/libs/1_61_0/doc/html/move/how_the_library_works.html">http://www.boost.org/doc/libs/1_61_0/doc/html/move/how_the_library_works.html</a> ("const rvalue and lvalues, bind to const ::boost::rv&lt; TYPE &gt;&amp;") </p> <p> My first guess for a fix would be based on: </p> <pre class="wiki"> #define BOOST_COPYABLE_AND_MOVABLE(TYPE)\ public:\ TYPE&amp; operator=(TYPE &amp;t)\ { this-&gt;operator=(*BOOST_MOVE_TO_RV_CAST(const ::boost::rv&lt;TYPE&gt;*, &amp;t)); return *this;}\ </pre><p> so what about: </p> <pre class="wiki"> #define BOOST_COPYABLE_AND_MOVABLE(TYPE)\ public:\ TYPE&amp; operator=(TYPE &amp;t)\ { this-&gt;operator=(*BOOST_MOVE_TO_RV_CAST(const ::boost::rv&lt;TYPE&gt;*, &amp;t)); return *this;}\ const TYPE&amp; operator=(const TYPE &amp;t)\ { this-&gt;operator=(*BOOST_MOVE_TO_RV_CAST(const ::boost::rv&lt;TYPE&gt;*, &amp;t)); return *this;}\ </pre><p> or similar. Not sure if this works for all cases or at all, but this would fulfill the "const lvalues bind to const rv&amp;" part. </p> <p> If this does not work, please add it to the docu. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Fri, 29 Jul 2016 09:56:52 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12307#comment:3 https://svn.boost.org/trac10/ticket/12307#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Documented limitation and possible workaround in commit: </p> <p> <a class="ext-link" href="https://github.com/boostorg/move/commit/cfd6be4ab46223917cb79e7dd856f582df587d7d"><span class="icon">​</span>https://github.com/boostorg/move/commit/cfd6be4ab46223917cb79e7dd856f582df587d7d</a> </p> Ticket