![](/img/trans.png)
[英]What does a template class without arguments, `template<>` mean?
[英]what does template<> (without any class T in the <>) mean?
我正在读取stl_construct.h中的一些源代码。在大多数情况下,它在<>中有某些行,我看到只有“ template<> ...
”的行。 这是什么?
这意味着接下来是模板专业化 。
猜猜,我完全误读了Q并回答了一些没有被问到的问题。
所以我在这里回答问的问题:
它是一个带有空模板参数列表的Explicit Specialization 。
当您使用给定的模板参数集实例化模板时,编译器会根据这些模板参数生成新定义。 但是有一种设施可以覆盖定义生成的这种行为。 而不是生成定义的编译器我们可以指定编译器应该为给定的模板参数集使用的定义。 这称为显式专业化 。
template<>
前缀表示以下模板声明不带模板参数。
显式专业化可应用于:
这是一个模板专业化,其中所有模板参数都已完全指定,并且<>
没有任何参数。
例如:
template<class A, class B> // base template
struct Something
{
// do something here
};
template<class A> // specialize for B = int
struct Something<A, int>
{
// do something different here
};
template<> // specialize both parameters
struct Something<double, int>
{
// do something here too
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.