模板消歧器 [英] template disambiguator

查看:240
本文介绍了模板消歧器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到任何关于使用模板关键字作为消歧器的信息,但没有什么。可能我搜索错误的关键字,但没有像.template或 - >模板在标准。 Google只显示来自不同论坛的GCC问题,但没有真正解释它使用的是什么。



在第11行(在GCC上) ,但我不能确定这是否符合标准。

 模板< 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


当成员模板专用化的名称出现在之后。或者在后缀表达式中,或者在限定id中的嵌套名称说明符之后,并且postfix-expression或qualified-id显式地取决于模板参数(14.6.2),成员模板名称必须是前缀为关键字模板。 否则该名称假定为非模板


PS:



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


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.

解决方案

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.

这篇关于模板消歧器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆