This commit is contained in:
Herb Sutter
2021-02-18 11:20:09 -08:00
parent a97be2d528
commit 64ab13b3d0

View File

@@ -4669,21 +4669,16 @@ If you don't want a generated default function, suppress it with `=delete`.
##### Example, good ##### Example, good
When a destructor needs to be declared just to make it `virtual`, it can be When a destructor needs to be declared just to make it `virtual`, it can be
defined as defaulted. To avoid suppressing the implicit move operations defined as defaulted.
they must also be declared, and then to avoid the class becoming move-only
(and not copyable) the copy operations must be declared:
class AbstractBase { class AbstractBase {
public: public:
virtual ~AbstractBase() = default; virtual ~AbstractBase() = default;
AbstractBase(const AbstractBase&) = default; // ...
AbstractBase& operator=(const AbstractBase&) = default;
AbstractBase(AbstractBase&&) = default;
AbstractBase& operator=(AbstractBase&&) = default;
}; };
Alternatively to prevent slicing as per [C.67](#Rc-copy-virtual), To prevent slicing as per [C.67](#Rc-copy-virtual),
the copy and move operations can all be deleted: `=delete` the copy and move operations and add a `clone`:
class ClonableBase { class ClonableBase {
public: public:
@@ -4693,6 +4688,7 @@ the copy and move operations can all be deleted:
ClonableBase& operator=(const ClonableBase&) = delete; ClonableBase& operator=(const ClonableBase&) = delete;
ClonableBase(ClonableBase&&) = delete; ClonableBase(ClonableBase&&) = delete;
ClonableBase& operator=(ClonableBase&&) = delete; ClonableBase& operator=(ClonableBase&&) = delete;
// ... other constructors and functions ...
}; };
Defining only the move operations or only the copy operations would have the Defining only the move operations or only the copy operations would have the