gpt4 book ai didi

c++ - 令人困惑的模板错误

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

我一直在玩 clang,我偶然发现了“test/SemaTemplate/dependent-template-recover.cpp”(在 clang 发行版中),它应该提供从模板错误中恢复的提示。

整个事情可以很容易地简化为一个最小的例子:

template<typename T, typename U, int N> struct X {
void f(T* t)
{
// expected-error{{use 'template' keyword to treat 'f0' as a dependent template name}}
t->f0<U>();
}
};

clang 产生的错误信息:

tpl.cpp:6:13: error: use 'template' keyword to treat 'f0' as a dependent template name
t->f0<U>();
^
template
1 error generated.

...但是我很难理解应该在哪里插入 template 关键字以使代码在语法上正确?

最佳答案

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.

t->f0<U>(); f0<U>是出现在 -> 之后的成员模板特化并且显式取决于模板参数U ,因此成员模板特化必须以 template 为前缀关键字。

所以改变t->f0<U>()t->template f0<U>() .

关于c++ - 令人困惑的模板错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3786360/

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