lundi 15 octobre 2018

Trivial compile time check for no copy constructor

A well-known idiom for making no-copy types is to create a base class

struct NoCopy {
   NoCopy(){}
   NoCopy(const NoCopy&) = delete;
   NoCopy& operator=(const NoCopy&) = delete;  
};

And derive from this, like so

struct Foo : NoCopy {
    Foo(){}
};

Which will make the following fail to compile

Foo f;
Foo f2 = f;

But how do I enforce this? Any derived class can do the following

struct Foo2 : NoCopy {
    Foo2(){}
    Foo2(const Foo2&){}
};

Which is perfectly legal but makes no sense, I have now a type which is both copyable and also not copyable (through its base class).

How do I avoid this?

Aucun commentaire:

Enregistrer un commentaire