CJ_Notned
CJ_Notned

Reputation: 308

Relation between automatic deletion of copy constructors and assignements

In our code base we were using this in past:

  Foo(const Foo&) = delete;
  Foo(Foo&&) = delete;
  Foo& operator=(const Foo&) = delete;
  Foo& operator=(Foo&&) = delete;

But as we were discussing it later we came to conclusion it is redundant and just:

  Foo(const Foo&) = delete;
  Foo(Foo&&) = delete;

Should be enough, because others are deleted automaticaly.

Is it 100% true even with different compilers and if yes is it possible to reduce it even more to delete for example just one of them and all others are auto deleted?

Thanks in advance.

Upvotes: 0

Views: 42

Answers (1)

Yksisarvinen
Yksisarvinen

Reputation: 22176

Yes and no.

Copy assignment operator is still generated with user declared copy constructor (or destructor), but this behaviour is deprecated and may be removed in future C++ standard versions.
Any sane compiler should issue a warning if such deprecated behaviour is relied on.

Move assignment operator will not be implicitly generated if either copy or move constructor is present.

Also, move constructor is not generated if you have a copy constructor declared, so

Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;

will be enough. However, it may still be worth to explicitly declare everything as deleted, just to make your intent clear.

Upvotes: 4

Related Questions