c++ - 模板消歧器

标签 c++ templates ambiguity

我正在尝试查找有关用作消歧器的模板关键字的任何信息,但对此一无所知。可能我正在搜索错误的关键字,但标准中没有 .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/

相关文章:

c++ - 上下文敏感性与歧义

c++ - 查找由智能指针引起的内存泄漏

c++ - 如何在 QT 中使用 QDomDocument 对象将字符串读取为 XML

java - 创建评估新创建的变量的代码模板

c++ - 如果不键入别名类型的完整声明,则无法从单独文件中的类模板定义访问类型别名

c++ - 在C++中为多继承组合类创 build 计的指南

c++ - 为什么 GDB 不能从带有信息符号的地址中找到符号?

c++ - 缓冲区溢出 - 似乎无法定位问题

javascript - 在 emberjs View 中使用 Action

xml - 架构中的内容模型不明确