我正在阅读stl_construct.h中的一些源代码,在大多数情况下,它在<>中有一些东西,我看到一些只有"template<> ...
"的行。这是什么?
很抱歉,我完全误读了问题并回答了一些没有被问到的内容。
现在我来回答被问到的问题:
这是一个空模板参数列表的显式特化。
当您使用给定的模板参数实例化模板时,编译器会基于这些模板参数生成新的定义。但是有一种方法可以覆盖定义生成的行为。我们可以指定编译器应该用于给定模板参数集的定义。这称为显式特化。
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
};