Consider the following code:
struct Test {
template <int S>
bool call();
};
template <>
bool Test::call<0>() {
return false;
}
template <>
bool Test::call<1>() {
return true;
}
template <int S, typename T>
static void func(T& t) {
t.call<S>();
}
int main()
{
Test t;
func<0>(t);
}
I got a compilation error:
a.cpp: In function ‘void func(T&)’:
a.cpp:19:15: error: expected primary-expression before ‘)’ token
a.cpp: In instantiation of ‘void func(T&) [with int S = 0; T = Test]’:
a.cpp:25:14: required from here
a.cpp:19:5: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
If I put t.call<0>() or t.call<1>() in the main() function, it works fine. Can someone tell me why template argument deduction doesn't work for this code? I am not sure why passing in a type with a partially-specialized template member function won't work in this case.
You need to say
template <int S, typename T>
static void func(T& t) {
t.template call<S>();
}
Because T is a dependent type name, the compiler doesn't know that call() is a template function, so you have to make it clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With