I have followed this post: Class template SFINAE to instantiate the template class conditionally.
That works perfectly for the classes which have only one template parameters, as shown in the link above.
However, I have two (template)arguments, and I would like to do certain SFINE check. Following is a minimal example of my code.
#include <type_traits>
#include <string>
template<class T, class U, class R> using arithmetic_types = std::enable_if_t<
std::is_arithmetic_v<T> &&
std::is_arithmetic_v<U>,
R
>;
template<class T, class U, class Enable = void> class MyClass;
template<class T, class U, arithmetic_types<T, U, void>>
class MyClass {
public:
MyClass() = default;
};
int main()
{
MyClass<int, int> o; // should work
MyClass<int, double> o1; // should work
MyClass<int, std::string> o2; // should be a complier error
return 0;
}
Above gave me the error message: https://godbolt.org/z/BEWJMp
error C3855: 'MyClass': template parameter 'Enable' is incompatible with the declaration
error C2079: 'o' uses undefined class 'MyClass'
error C2079: 'o1' uses undefined class 'MyClass'
error C2079: 'o2' uses undefined class 'MyClass'
Unfortunately, I could not understand the error message(error C3855:
).
Why I can't do the same principle shown in the above link to more template parameters?
And what is the best solution for this?