5
struct Foo
{
    Foo() = default;
    Foo(Foo&&) = default;
};

int main()
{
    Foo a, b;
    a = b;
   // ^ 

   return 0;
}

error: use of deleted function 'Foo& Foo::operator=(const Foo&)'

in g++4.6 -std=c++0x it's ok. but, in g++6.2 -std=c++11 it's error. why?

Bruce
  • 80
  • 1
  • 6
  • 1
    Read further : "*note: ‘constexpr Foo& Foo::operator=(const Foo&)’ is implicitly declared as deleted because ‘Foo’ declares a move constructor or move assignment operator*". And that is because of the standard says so : http://stackoverflow.com/questions/11255027/why-user-defined-move-constructor-disables-the-implicit-copy-constructor – Hatted Rooster May 13 '17 at 15:28

2 Answers2

9

The answer is because the C++ standard says so:

[class.copy]

If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4).

You can always declare a default copy constructor, in your case:

 Foo(const Foo&) = default;
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
4

Sam has explained why GCC 6.2's behaviour is correct.

GCC 4.6's behaviour is simply due to incomplete C++11 support in that version (as evidenced by the "experimental" C++0x switch); C++11 was not fully supported until 4.8.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055