tangy
tangy

Reputation: 3256

Rule of 4.5: No move assignment operator?

Based on the popular question What is the copy-and-swap idiom?:

  1. Why does the rule of 4.5 not include the move assignment operator (to effectively become the rule of 5.5)? Instead I've read (eg. here What is the Rule of Four (and a half)?) that we either have rule of 4.5 or 5?

  2. Since the swap member function is noexcept, shouldnt the copy assignment operator also be marked the same (the move constructor can't since it calls the default constructor that can throw)?

dumb_array& operator=(dumb_array other) // should be noexcept?
{
    swap(*this, other);
    return *this;
}

Upvotes: 1

Views: 184

Answers (1)

Phil1970
Phil1970

Reputation: 2623

Because it would not be useful.

Click your second link then forth link there The Rule of The Big Four (and a half) – Move Semantics and Resource Management

Read carefully section 5 – Move assignment.

You will see

Eliminating the move assignment operator

In reality is the move assignment operator is unnecessary!

with all the explanation!

Essentially the operator dumb_array& operator=(dumb_array other) will be used when normally you would have used move assignment operator.

I haven't verified but you might be able to delete it too as it will not be generated anyway.

Upvotes: 2

Related Questions