c++编译器将模板语法视为 '<'操作员
我正在尝试编译以下代码:
struct A {
template<int N> static void a() {}
};
template<> void A::a<5>() {}
template<class T>
struct B {
static void b() {
T::a<5>();
}
};
void test() {
A::a<5>();
B<A>::b();
}
编译器将 T::a<5>
中的 <
解释为运算符 <
,导致错误:
invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
是否有任何方法可以显式实例化 T::a<5>
而不会出现编译器错误?
谢谢。
gcc 版本 4.5.1 20100924(红帽 4.5.1-4)(GCC)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,将该行更改为:
编译器不知道
T::a
是否是一个函数(因为其template
性质)。通过提及template
,您可以明确地通知编译器。这个问题被问了很多次,这里是其中之一。Yes, change that line to:
Compiler doesn't know if
T::a
is a function (because of itstemplate
nature). By mentioningtemplate
, you inform compiler explicitly. This question is asked many times, here is one of them.