7
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?

1
  • 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 : stackoverflow.com/questions/11255027/… Commented May 13, 2017 at 15:28

2 Answers 2

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; 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.