为什么此代码会给出错误,“template specialization requires”template<>'“? [英] Why does this code give the error, "template specialization requires 'template<>'"?

查看:1217
本文介绍了为什么此代码会给出错误,“template specialization requires”template<>'“?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Clang编译时

  template< class T& 
struct字段
{
char const * name;
Field(char const * name):name(name){}
};

template< class Derived>
class CRTP {static Field< Derived> const _field; };

class Class:public CRTP< Class> {};
字段< Class> const CRTP< Class> :: _ field(blah);

int main(){}

我得到

 错误:模板专门化需要'template'>'
字段< Class> const CRTP< Class> :: _ field(blah);
~~~~~~~~~~~~ ^

的错误。我的 _field 定义有什么问题,我该如何解决?



code> _field 对于所有子类不一定相同。)

解决方案

编译器将此标识为模板专用化(例如,为了能够检查语法),需要模板关键字:

 模板<> 
字段< Class> const CRTP< Class> :: _ field(blah);

它的括号是空的,因为所有的模板参数是专门的,但你不能把它留下来。 p>

When I try to compile this with Clang

template<class T>
struct Field
{
    char const *name;
    Field(char const *name) : name(name) { }
};

template<class Derived>
class CRTP { static Field<Derived> const _field; };

class Class : public CRTP<Class> { };
Field<Class>   const CRTP<Class>::_field("blah");

int main() { }

I get

error: template specialization requires 'template<>'
Field<Class>   const CRTP<Class>::_field("blah");
                     ~~~~~~~~~~~  ^

I don't understand the error at all. What is wrong with my definition of _field and how do I fix it?

(Note that the arguments to _field are not necessarily the same for all subclasses.)

解决方案

For the compiler to identify this as a template specialization (e.g. to be able to check the syntax), you need the template keyword:

template<>
Field<Class> const CRTP<Class>::_field("blah");

Its brackets are empty as all template parameters are specialized, but you cannot just leave it away.

这篇关于为什么此代码会给出错误,“template specialization requires”template&lt;&gt;'“?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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