c++ - Can std::enable_if methods be defined outside of a class - Stack Overflow

Is there a syntax that allows for method1 to be defined outside of the class or is that not SFINAE frie

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

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() {}
      |                              ^
Share Improve this question edited Nov 19, 2024 at 16:23 asimes asked Nov 19, 2024 at 16:16 asimesasimes 6,1365 gold badges44 silver badges80 bronze badges 7
  • 1 Please include the error message. – cigien Commented Nov 19, 2024 at 16:18
  • 2 Please don't omit #include. Why do you make other users guess what you missed? – 3CxEZiVlQ Commented Nov 19, 2024 at 16:20
  • 4 The entire error message is much clearer godbolt./z/neG9PPd63 Just remove the = TClass from the definition. – cigien Commented Nov 19, 2024 at 16:22
  • 2 The reason you include stuff like #include is because you don't know what is going wrong. If you knew what was going wrong, you could work out what you can safely exclude; but then why ask the question? So you should aim for a minimal reproducible example, code that can be copy/pasted and reproduces your error, as best you can. Which includes "obviously not the problem" code like #includes, because messing those up can give strange errors, and because it makes replicating your problem easier for other people to debug. – Yakk - Adam Nevraumont Commented Nov 19, 2024 at 16:25
  • 1 In the out of class definition, you cannot reintroduce the default value TMethod = TClass. It is just like a function with a default parameter: you define it only in declaration, – zdf Commented Nov 19, 2024 at 16:28
 |  Show 2 more comments

1 Answer 1

Reset to default 2

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742414546a4439483.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信