1

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
tangy
  • 3,056
  • 2
  • 25
  • 42

1 Answers1

2

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.

Community
  • 1
  • 1
Phil1970
  • 2,605
  • 2
  • 14
  • 15
  • Thanks. Could you also clarify the second question about making the copy assignment noexcept? – tangy Jan 06 '19 at 03:50
  • 1
    `noexcept` should not be there. `dumb_array other` constructs a copy or moves an object depending on lvalue or rvalue passed. Both are not noexcept. – 273K Jan 06 '19 at 07:30