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:
我想知道在哪里使用模板和typename,我遇到了一个我无法解决的问题。我有一个模板函数f
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源。cpp:在函数‘void f()’中:源。错误:非模板'f'用作模板源。注意:使用'typename T:::C::template f'表示它是一个模板
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”。所以我按照以下的方式做了更改:
// ...
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'
错误:在“struct A::C”中没有名为“f”的类模板
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?
我认为这个错误是不正确的,因为在结构a中有一个名为f的公共模板函数::C。我在这里做错了什么?
1 个解决方案
#1
3
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:
假设我们使f为静态的,这样就可以不使用实例来调用它,那么就不需要typename,因为您没有创建任何依赖类型的歧义(例如,C不能是变量,因为您使用它与:::just after)一起使用)。这里有正确的语法:
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>()
:
如果不希望它是静态的,可以创建a::C并使用.template f
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>();
}
#1
3
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:
假设我们使f为静态的,这样就可以不使用实例来调用它,那么就不需要typename,因为您没有创建任何依赖类型的歧义(例如,C不能是变量,因为您使用它与:::just after)一起使用)。这里有正确的语法:
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>()
:
如果不希望它是静态的,可以创建a::C并使用.template f
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>();
}