2015-04-01 85 views
0

我想使部分专业化类可以从类模板继承,但编译会给出错误。代码和错误如下。谁可能从类模板继承部分专业化

template<class t> 
class a{}; 

template<class t> 
class b<t*>:a<t>{}; 

main(){ 
    a<int> obj1; 
    b<int*> obj2; 
} 

错误:

error: 'b' is not a template 
error: expected class-name before '{' token 

当我更换B级代码,此代码

class b:public a<t>{}; 

它的工作原理。

回答

2

为了部分专门化模板,您必须已经创建了主模板。如果您想要定义b<T*>,则必须先定义b

也就是说,下面是一个错误:

template<class t> 
class a{}; 

template<class t> 
class b<t*>:a<t>{}; 

这是有效的:

template<class t> 
class a{}; 

// Primary template 
template<class t> 
class b{}; 

// Partially specialized for pointers 
template<class u> 
class b<u*>:a<u>{};