混淆模板错误 [英] Confusing Template error

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

问题描述

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

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

  template< typename T,typename U,int N> struct X {
void f(T * t)
{
// expected-error {{使用'template'关键字将'f0'作为依赖模板名}}
t→f 0 U();
}
};

clang生成的错误消息:

  tpl.cpp:6:13:错误:使用'template'关键字来将'f0'视为依赖模板名称
t-> f0&
^
模板
生成了1个错误。

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

解决方案

ISO C ++ 03 14.2 / 4:


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


t-> f0< U> (); f0 是在 - > ,并且显式地依赖于模板参数 U ,因此成员模板特化必须以 template p>

因此请将 t-> f0< U>()更改为 t->模板f0 U()


I've been playing with clang a while, and I stumbled upon "test/SemaTemplate/dependent-template-recover.cpp" (in the clang distribution) which is supposed to provide hints to recover from a template error.

The whole thing can be easily stripped down to a minimal example:

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>();
    }
};

The error message yielded by clang:

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

... But I have a hard time understanding where exactly one is supposed to insert the template keyword to have the code to be syntactically correct?

解决方案

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.

In t->f0<U>(); f0<U> is a member template specialization which appears after -> and which explicitly depends on template parameter U, so the member template specialization must be prefixed by template keyword.

So change t->f0<U>() to t->template f0<U>().

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

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