Question

I've been using Google's DISALLOW_COPY_AND_ASSIGN macro from their C++ Style Guide for a couple of months now, but it recently occurred to me that it would be useful to additionally disable the move constructor and move assignment.

I haven't written any real macros before (in fact, I've been trying to stay away from them as much as possible), so I'd like to get some feedback from the rest of you on whether I've implemented it correctly.

// Original Version
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&);                 \
void operator=(const TypeName&)

// Modified Version (no move semantics)
#define DISALLOW_COPY_MOVE_AND_ASSIGN(TypeName) \
TypeName(const TypeName&);                      \
void operator=(const TypeName&);                \
TypeName(TypeName&&);                           \
void operator=(const TypeName&&)

Suggestions and criticism are very welcome.

Thanks for your time!

Was it helpful?

Solution

There is no need to disable the move constructor or the move assignment if there is already a declared copy constructor or copy assignment: the move constructor and the move assignment are only implicitly declared as defaulted if there is neither copy constructor nor copy assignment assignment declared for the class. It seems safe to leave the macro alone for that purpose.

A potential change, however, could be to declare the copy constructor and copy assignment explicitly as deleted:

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(TypeName&) = delete;              \
void operator=(TypeName) = delete;

This way the copy constructor and the copy assignment can neither be called from members of the class nor can they they be defined.

In C++ (starting with the 2011 revision) I can't see any point in this macro: it seems easy enough to just delete the constructors explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top