I'm trying to select a constructor through SFINAE as following:
template<typename T>
class MyClass
{
public:
template<typename C, typename = std::enable_if_t<std::is_class<C>::value>>
MyClass(C) { }
template<typename C, typename = std::enable_if_t<std::is_pointer<C>::value>>
MyClass(C) { }
};
but the compiler complains with following error:
error C2535: 'MyClass::MyClass(C)': member function already defined or declared
without even instantiating the constructor.
I worked out a working but ugly solution which i don't want to use because of the extra unused parameter:
template<typename T>
class MyWorkingClass
{
public:
template<typename C>
MyWorkingClass(C, std::enable_if_t<std::is_class<C>::value>* = nullptr) { }
template<typename C>
MyWorkingClass(C, std::enable_if_t<std::is_pointer<C>::value>* = nullptr) { }
};
A short usage example is given here:
void* ptr = nullptr;
MyClass<int> mc1(ptr);
std::vector<int> vec;
MyClass<int> mc2(vec);
// Shall raise an error
// MyClass<int> mc2(0);
The traits std::is_pointer
and std::is_class
are just an example, the original traits are more complicated.
Is there a way to select the constructor through SFINAE without adding another parameter to the constructor (maybe very close to the first appproach)?