I have this kind of structure for a template class.
My aim is to forbid creation of those class which not provide a full specialization:
class AbstractContainer
{
public:
virtual ~AbstractContainer() = default;
// declare here interface methods
};
template <class T>
class Container final : public AbstractContainer
{
public:
Container() = delete;
};
template <>
class Container<int> : public AbstractContainer
{
public:
Container() = default;
explicit Container(const int& type) : AbstractContainer(), type_(type){}
private:
int type_;
};
Everyhthing works fine
Container<int> a; // it works
Container<int> a(5); // it works
Container<char> a; // does not compile
but I noticed it compiles for these case
Container<int> a(Container<char>());
Container<int> a(Container<CustomClass>());
How can I avoid this situation? I do want a copy constructor but not with the wrong type, ideally I would like to have the same issue of compile error (I could use an assert somewhere, but don't know how to set up it).