I have the following Singleton policy-class implementation:
template <typename T>
class Singleton
{
Singleton(){}; // so we cannot accidentally delete it via pointers
Singleton(const Singleton&) = delete; // no copies
Singleton& operator=(const Singleton&) = delete; // no self-assignments
Singleton(Singleton&&) = delete; // WHY?
Singleton& operator=(Singleton&&) = delete; // WHY?
public:
static T& getInstance() // singleton
{
static T instance; // Guaranteed to be destroyed.
// Instantiated on first use.
// Thread safe in C++11
return instance;
}
};
which I then use via the curiously recurring template pattern (CRTP)
class Foo: public Singleton<Foo> // now Foo is a Singleton
{
friend class Singleton<Foo>;
~Foo(){}
Foo(){};
public:
// rest of the code
};
I cannot figure out why I should delete the move constructor and assignment operator. Can you give me a single example where I end up breaking the singleton if I don't delete (don't define at all) the move ctor and assignment operator?
If you declare a copy constructor (even if you define it as delete
d in the declaration), no move constructor will be declared implicitly. Cf. C++11 12.8/9:
If the definition of a class
X
does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only ifā X does not have a user-declared copy constructor,
ā ...
Since you do have a user-declared copy constructor, there won't be a move constructor at all if you don't declare one. So you can just get rid of the move constructor declaration-definition entirely. Same for the move-assignment operator.