I'm trying to understand where to use template and typename and I came across a problem I can't quite get around. I have a template-function f<T> which uses the type passed to it (which will be a class) to call the template-member-function .f<T>. I think my use of typename in the function body is correct, however, I keep getting the following error:
source.cpp: In function
'void f()':
source.cpp:11:19: error: non-template'f'used as template
source.cpp:11:19: note: use'typename T::C::template f'to indicate that it is a template
struct A {
struct C {
template <typename T> void f() {}
};
};
template <typename T> void f() {
typename T::C::f<int>();
}
int main() {
f<A>();
}
Notice how on the last error it advises to use 'typename T::C::template f' instead. So I made the following change in accordance:
// ...
typename T::C::template f<int>();
// ...
I did as it said, but then I received the next line of error:
error: no class template named
'f'in'struct A::C'
I believe this error is incorrect in that there is in fact a public template function named f in struct A::C. What could I be doing wrong here?
Assuming we make f static so it can be called without an instance, you don't need typename because you're not creating any ambiguities with a dependent type (i.e. C can't be a variable because you use it with :: just after). Here is the correct syntax:
struct A {
struct C {
template <typename T>
static void f() {}
};
};
template <typename T> void f() {
T::C::template f<int>();
}
If you wanted not to make it static, you'd create a A::C and use .template f<int>():
struct A {
struct C {
template <typename T>
static void f() {}
};
};
template <typename T> void f() {
typename T::C c;
//^^^^^^^^ Now we DO need typename to disambiguate.
c.template f<int>();
}
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