Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么专用模板类需要转发声明?_C++_Templates - Fatal编程技术网

C++ 为什么专用模板类需要转发声明?

C++ 为什么专用模板类需要转发声明?,c++,templates,C++,Templates,守则如下: template<typename,int> class Uoo; //without this will result in complie error,why? template<typename T> class Uoo<T,1> { }; int main(){ return 0; } 模板类Uoo//如果没有这一点,将导致complie错误,为什么? 模板 Uoo类 { }; int main(){ 返回0; } 为什么

守则如下:

template<typename,int> class Uoo;  //without this will result in complie error,why?

template<typename T>
class Uoo<T,1>
{
};

int main(){
    return 0;
}
模板类Uoo//如果没有这一点,将导致complie错误,为什么?
模板
Uoo类
{
};
int main(){
返回0;
}

为什么专用模板类需要转发声明?

您实际上并不是在做转发声明。您要做的是首先定义模板类的“模式”,然后定义它的专用上下文或版本。更好的问题是,如果没有非专门化的案例,那么第二个模板参数有什么意义?

以下代码是模板的专门化

template<typename T>
class Uoo<T,1>
{
};
模板
Uoo类
{
};
但是你没有说什么是非专业化的形式,语言要求你这样做。因此,您需要添加原型:

template<typename,int> class Uoo;
模板类Uoo;

您实际上不需要声明非专用表单,因为它的实例从来都不是必需的。因此,一个原型就足够了。

如前所述,
模板类Uoo
模板类Uoo
的部分专门化;它将int参数固定为1。它不能是不存在的模板的部分专门化

您可以通过编写

template<typename T>
class Uoo
{
...
};
模板
Uoo类
{
...
};

那么,这种专业化实际上会专业化什么?要专门化一些东西,必须有一些“通用”的东西可以专门化。很抱歉,我不太明白你说的让模板类自给自足是什么意思。他的原始类需要两个模板参数。不需要另一个类。原始代码中定义的唯一一种情况将int-template参数固定为1,因此不需要将其作为模板参数。只需要typename参数,两个类是冗余的。