gpt4 book ai didi

c++ - 模板消歧器

转载 作者:IT老高 更新时间:2023-10-28 23:19:10 25 4
gpt4 key购买 nike

我正在尝试查找有关用作消歧器的模板关键字的任何信息,但对此一无所知。可能我正在搜索错误的关键字,但标准中没有 .template 或 ->template 之类的东西。 Google 仅显示来自不同论坛的 GCC 问题,但并未真正解释它的用途。

这样的代码在第 11 行(在 GCC 上)没有模板关键字时无法编译,但我不太确定这是否符合标准。

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

所以我的问题是:为什么在这里使用模板关键字,没有该关键字会有什么样的歧义,我在哪里可以阅读(我非常感谢标准的链接)。

谢谢。

最佳答案

简答:因为标准是这样说的

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.

附注:

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

关于c++ - 模板消歧器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4077110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com