模板消歧器
我试图找到有关用作消歧器的模板关键字的任何信息,但没有任何信息。可能我正在搜索错误的关键字,但标准中没有像 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短回答:因为标准是这么说的
ISO C++03
14.2/4
PS:
如果没有额外使用模板,编译器不知道后面的小于标记
(<)
并不是真正的“小于”,而是模板参数列表的开头。Short answer : Because the standard says so
ISO C++03
14.2/4
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.