Maestro
Maestro

Reputation: 2572

What does it mean move operations are never defined as deleted functions

I am reading C++ primer 5 edition. until chapter 13 when talking about "move operations":

Unlike the copy operations, a move operation is never implicitly defined as a deleted function. However, if we explicitly ask the compiler to generate a move operation by using = default (§ 7.1.4, p. 264), and the compiler is unable to move all the members, then the move operation will be defined as deleted. With one important exception, the rules for when a synthesized move operation is defined as deleted are analogous to those for the copy operations (§ 13.1.6, p. 508):

  • Unlike the copy constructor, the move constructor is defined as deleted if the class has a member that defines its own copy constructor but does not also define a move constructor, or if the class has a member that doesn’t define its own copy operations and for which the compiler is unable to synthesize a move constructor. Similarly for move-assignment.
  • The move constructor or move-assignment operator is defined as deleted if the class has a member whose own move constructor or move-assignment operator is deleted or inaccessible.
  • Like the copy constructor, the move constructor is defined as deleted if the destructor is deleted or inaccessible.
  • Like the copy-assignment operator, the move-assignment operator is defined as deleted if the class has a const or reference member.

Upvotes: 0

Views: 257

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

Does this mean copy operations are defined implicitly as deleted operations? if so when?

Yes.

When the members cannot be copied (e.g. they are of non-copyable types).


So I don't understand "unlike the copy operations, a move operation is never implicitly defined as a deleted function".

  • When the members can't be copied, the copy constructor is deleted.
  • When the members can't be moved, the move constructor is not deleted. Instead, it simply doesn't exist, so a copy is performed instead.
    • Unless the copy constructor was deleted! Then you just can't do anything.

If the move constructor were deleted, there would be an immediate compilation error, not an attempt to use the copy constructor instead.


In other words please explain the difference between implicit move operations and their corresponding copy ones.

The key here is the difference between not declaring something, and declaring it as deleted.


I don't know why the book's making such a big deal out of this. The difference is only interesting if it's interesting that the copy constructor gets implicitly deleted sometimes. And it's not particularly interesting that the copy constructor is deleted, because if it weren't, you still just wouldn't get a copy. There'd be no other constructor to fall back on. Well, I suppose, given some other implicit conversion sequences I suppose there could be, so that's a little interesting.

Upvotes: 1

Related Questions