diff --git a/format-test.cc b/format-test.cc index 42ef65fb..542da0f1 100644 --- a/format-test.cc +++ b/format-test.cc @@ -1634,8 +1634,7 @@ TEST(FormatIntTest, FormatDec) { class File { private: int fd_; - File(const File &); - void operator=(const File &); + FMT_DISALLOW_COPY_AND_ASSIGN(File); public: File(int fd) : fd_(fd) {} ~File() { close(fd_); } diff --git a/format.h b/format.h index 6e0f45e6..744dbafa 100644 --- a/format.h +++ b/format.h @@ -99,6 +99,12 @@ # define FMT_NOEXCEPT(expr) #endif +// A macro to disallow the copy constructor and operator= functions +// This should be used in the private: declarations for a class +#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&); \ + void operator=(const TypeName&) + #if _MSC_VER # pragma warning(push) # pragma warning(disable: 4521) // 'class' : multiple copy constructors specified @@ -165,9 +171,7 @@ class Array { } } - // Do not implement! - Array(const Array &); - void operator=(const Array &); + FMT_DISALLOW_COPY_AND_ASSIGN(Array); public: Array() : size_(0), capacity_(SIZE), ptr_(data_) {} @@ -1266,9 +1270,7 @@ class BasicFormatter { // // This is done because BasicFormatter objects should normally exist // only as temporaries returned by one of the formatting functions. - // Do not implement. - BasicFormatter(const BasicFormatter &); - BasicFormatter& operator=(const BasicFormatter &); + FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter); protected: const Char *TakeFormatString() { @@ -1405,9 +1407,7 @@ class Formatter : private Action, public BasicFormatter { BasicWriter writer_; bool inactive_; - // Forbid copying other than from a temporary. Do not implement. - Formatter(const Formatter &); - Formatter& operator=(const Formatter &); + FMT_DISALLOW_COPY_AND_ASSIGN(Formatter); public: /**