Is there a syntax that allows for method1
to be defined outside of the class or is that not SFINAE friendly?
#include <type_traits>
template <int TClass>
struct SampleClass {
// This definition compiles just fine
template <int TMethod = TClass>
typename std::enable_if<TMethod == 0, void>::type
method0() {}
// Declaration
template <int TMethod = TClass>
typename std::enable_if<TMethod == 1, void>::type
method1();
};
// This does not compile
template <int TClass>
template <int TMethod = TClass>
typename std::enable_if<TMethod == 1, void>::type
SampleClass<TClass>::method1() {}
int main() { return 0; }
Using GCC 10.2.1, error:
foo.cpp:20:30: error: default argument for template parameter for class enclosing 'typename std::enable_if<(TMethod == 1), void>::type SampleClass<TClass>::method1()'
20 | SampleClass<TClass>::method1() {}
| ^
The error message describes the problem:
foo.cpp:20:30: error: default argument for template parameter for class enclosing 'typename std::enable_if<(TMethod == 1), void>::type SampleClass<TClass>::method1()' 20 | SampleClass<TClass>::method1() {} |
Specifically "default argument for template parameter" is the problem, which is what the error message points to.
The problem is the default argument. You aren't allowed to put it here.
template <int TClass>
template <int TMethod /* = TClass*/>
typename std::enable_if<TMethod == 1, void>::type
SampleClass<TClass>::method1() {}
Just comment it out.
Generally repeating default arguments is banned in C++ to avoid making the compiler check that the two cases have the same value.