move-assignment operator is defined as deleted if the class has a const member?

Viewed 120

It says "Like the Copy-assignment operator, the move-assignment operator is defined as deleted if the class has a const or reference member." in C++ Primer 5th Section 13.6 .

And I write a demo like below.

#include <iostream>

class A {
public:
    A() : mem(0) {}
    const int mem;
};

int main()
{
    A a1, a2 = std::move(a1);
    return 0;
}

It has const member in class A but why it can be compiled successfully? What I missed?

1 Answers

Because this is not assigment, but initialization. a2 is initialized from std::move(a1) via the move constructor, but not move assignment operator.

You'll get the error if you change the code to:

A a1;
A a2;
a2 = std::move(a1); // assignment
Related