Force type of C++ template

c++, templates

Solution

Another version is to leave it undefined for the forbidden types

template<typename T>
struct Allowed; // undefined for bad types!

template<> struct Allowed<std::string> { };
template<> struct Allowed<short> { };

template<typename T>
struct MyClass : private Allowed<T> { 
  // ...
};

MyClass<double> m; // nono

Problem

I've a basic template class, but I'd like to restrain the type of the specialisation to a set of classes or types. e.g.: ``` template <typename T> class MyClass { .../... private: T* _p; }; MyClass<std::string> a; // OK MYCLass<short> b; // OK MyClass<double> c; // not OK ``` Those are just examples, the allowed types may vary. Is that even possible? If it is, how to do so? Thanks.

Original source

Related problems