模板消歧器

发布于 2024-09-30 02:48:26 字数 550 浏览 0 评论 0原文

我试图找到有关用作消歧器的模板关键字的任何信息,但没有任何信息。可能我正在搜索错误的关键字,但标准中没有像 .template 或 ->template 这样的东西。谷歌只显示来自不同论坛的 GCC 问题,但没有真正解释它的用途。

类似的代码在第 11 行(在 GCC 上)没有 template 关键字的情况下无法编译,但我不太确定这是否符合标准。

template<typename B>
struct S1
{
    template<typename T> void test() {}
};

template<typename T>
struct S2
{
    S2()
    {
        S1<T>().template test<int>();
    }
};

int main()
{
   S2<int>();
}

所以我的问题是:为什么这里使用 template 关键字,如果没有该关键字会有什么样的歧义,我在哪里可以读到相关内容(我真的很感激标准的链接)。

谢谢。

I'm trying to find any information about template keyword used as disambiguator, but there is nothing about that. Probably I'm searching wrong keywords, but there is nothing like .template or ->template in standard. Google shows only GCC problems from different forums, but not really explanation what is it used for.

Code like that failed to compile without template keyword on line 11 (on GCC), but I'm not quite sure that this conforms standard.

template<typename B>
struct S1
{
    template<typename T> void test() {}
};

template<typename T>
struct S2
{
    S2()
    {
        S1<T>().template test<int>();
    }
};

int main()
{
   S2<int>();
}

So my question is: why does template keyword used here, what kind of ambiguity is there without that keyword and where can I read about that (I would really appreciate link to standard).

Thanks.

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

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

发布评论

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

评论(1

冷情妓 2024-10-07 02:48:26

简短回答:因为标准是这么说的

ISO C++03 14.2/4

当成员模板专业化的名称出现在 后时。或->在后缀表达式中,或在限定 ID 中的嵌套名称说明符之后,并且后缀表达式或限定 ID 显式依赖于模板参数 (14.6.2),成员模板名称必须以关键字模板。 否则,该名称将被假定为命名非模板

PS:

如果没有额外使用模板,编译器不知道后面的小于标记 (<) 并不是真正的“小于”,而是模板参数列表的开头。

Short answer : Because the standard says so

ISO C++03 14.2/4

When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

P.S:

Without that extra use of template, the compiler does not know that the less-than token (<) that follows is not really "less than" but the beginning of a template argument list.

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