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
|
Show 2 more comments
1 Answer
Reset to default 2The 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
= TClass
from the definition. – cigien Commented Nov 19, 2024 at 16:22#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#include
s, 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:25TMethod = 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