Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>q3 of the Original Poster</strong></p> <p>I think you (and some of the other responders) misunderstood what the compiler error meant, and came to the wrong conclusions because of it. The compiler thinks that the (move) assignment call is ambiguous, and it's right! You have multiple methods that are equally qualified.</p> <p>In your original version of the <code>AnObject</code> class, your copy constructor takes in the old object by <code>const</code> (lvalue) reference, while the assignment operator takes its argument by (unqualified) value. The value argument is initialized by the appropriate transfer constructor from whatever was on the right-side of the operator. Since you have only one transfer constructor, that copy constructor is always used, no matter if the original right-side expression was a lvalue or a rvalue. This makes the assignment operator act as the copy-assignment special member function.</p> <p>The situation changes once a move constructor is added. Whenever the assignment operator is called, there are two choices for the transfer constructor. The copy constructor will still be used for lvalue expressions, but the move constructor will be used whenever a rvalue expression is given instead! This makes the assignment operator <em>simultaneously</em> act as the move-assignment special member function.</p> <p>When you added a traditional move-assignment operator, you gave the class two versions of the same special member function, which is an error. You already have what you wanted, so just get rid of the traditional move-assignment operator, and no other changes should be needed.</p> <p>In the two camps listed in your update, I guess I'm technically in the first camp, but for entirely different reasons. (Don't skip the (traditional) move-assignment operator because it's "broken" for your class, but because it's superfluous.)</p> <p>BTW, I'm new to reading about C++11 and StackOverflow. I came up with this answer from browsing another S.O. question before seeing this one. (<strong>Update</strong>: Actually, I still had the <a href="https://stackoverflow.com/questions/3106110/can-someone-please-explain-move-semantics-to-me/3109981#3109981">page</a> open. The link goes to the specific response by <a href="https://stackoverflow.com/users/252000/fredoverflow">FredOverflow</a> that shows the technique.)</p> <p><strong>About the 2011-May-12 Response by Howard Hinnant</strong></p> <p>(I'm too much of a newbie to directly comment on responses.)</p> <p>You don't need to explicitly check for self-assignment if a later test would already cull it. In this case, <code>n != rh.n</code> would already take care of most of it. However, the <code>std::copy</code> call is outside of that (currently) inner <code>if</code>, so we would get <code>n</code> component-level self-assignments. It's up to you to decide if those assignments would be too anti-optimal even though self-assignment should be rare.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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