Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can std::enable_if methods be defined outside of a class

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() {}
      |                              ^

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading