c++编译器将模板语法视为 '<'操作员

发布于 2024-12-18 11:51:14 字数 636 浏览 2 评论 0 原文

我正在尝试编译以下代码:

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)

I'm trying to compile the following code:

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();
}

and compiler interprets < in T::a<5> as an operator <, resulting in error:

invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’

Is there any way to explicitly instantiate T::a<5> without compiler errors?
Thank you.

gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-12-25 11:51:14

是的,将该行更改为:

T::template a<5>();

编译器不知道 T::a 是否是一个函数(因为其 template 性质)。通过提及template,您可以明确地通知编译器。这个问题被问了很多次,这里是其中之一

Yes, change that line to:

T::template a<5>();

Compiler doesn't know if T::a is a function (because of its template nature). By mentioning template, you inform compiler explicitly. This question is asked many times, here is one of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文