【发布时间】:2020-05-20 14:05:00
【问题描述】:
我有以下代码:
template <typename TC>
class C
{
struct S
{
template <typename TS>
void fun() const
{}
};
void f(const S& s)
{
s.fun<int>();
}
};
// Dummy main function
int main()
{
return 0;
}
当同时使用 gcc 9.2 和 clang (9.0) 构建它时,由于调用 fun 需要 template 关键字,我遇到了编译错误。 Clang 显示:
error: use 'template' keyword to treat 'fun' as a dependent template name
s.fun<int>();
^
template
我不明白为什么编译器认为fun 是f 上下文中的从属名称,因为f 本身不是模板。如果我将C 更改为常规类而不是模板,则错误消失;但是,我不明白为什么首先应该出现错误,因为 S 和 f 都不依赖于 TC。
奇怪的是,MSVC 19.22 编译得很好。
注意
在投票结束作为 Where and why do I have to put the "template" and "typename" keywords? 的骗子之前,请考虑这是一种特殊情况,即使 S 确实是一个从属名称,在 f 的上下文中,如果不是因为以下事实,它也不会是依赖的它们是当前实例化的成员。
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: c++ templates language-lawyer dependent-name