0

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.
  • So I don't understand "unlike the copy operations, a move operation is never implicitly defined as a deleted function".

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

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

Community
  • 1
  • 1
Maestro
  • 2,512
  • 9
  • 24
  • Your text should answer your second bullet point somewhere (maybe in the section about copy operations, 13.1.6 p.508 perhaps?) – M.M Feb 02 '20 at 20:06
  • 1
    "The move constructor is defined as deleted if the destructor is deleted or inaccessible." is wrong , [see here for the actual list](https://en.cppreference.com/w/cpp/language/move_constructor#Deleted_implicitly-declared_move_constructor) – M.M Feb 02 '20 at 20:11
  • @M.M: I don't know – Maestro Feb 02 '20 at 20:11
  • @M.M: I found this in your link: `T has direct or virtual base class with a deleted or inaccessible destructor;` – Maestro Feb 02 '20 at 20:16
  • yes, the text in your question doesn't mention base classes, unless you misquoted it – M.M Feb 02 '20 at 20:23
  • @M.M: I've just copied it just as it is. I don't understand "move operation is never defined as a deleted function"?? – Maestro Feb 02 '20 at 20:32
  • 1
    Yeah it seems like a mistake in the book based on what you have posted, although maybe there is further context in the book. The book should go on to explain the points it is making – M.M Feb 02 '20 at 20:43
  • The book is not good at special member functions. Specifically: when they are implicitly-declared, what are user-declared and user-provided, and when they are defined as deleted, etc – rosshjb Nov 20 '21 at 17:45

1 Answers1

1

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.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35