1. 简介
局部特化(partial specialization)容许为给定类别的模板实参自定义类模板和变量模板.
2. 语法
template<parameter-list> class ClassName<argument-list> declaration
其中,argument-list
能够蕴含具体的类型(对应类型参数)、具体的值(对应非类型参数),也能够蕴含在 <parameter-list>
中指定的模板形参.
如,
// primary template
template<class T1, class T2, int I>
class A {};
// #1: partial specialization where T2 is a pointer to T1
template<class T, int I>
class A<T, T*, I> {};
// #2: partial specialization where T1 is a pointer
template<class T, class T2, int I>
class A<T*, T2, I> {};
// #3: partial specialization where T1 is int, I is 5, and T2 is a pointer
template<class T>
class A<int, T*, 5> {};
// #4: partial specialization where T2 is a pointer
template<class X, class T, int I>
class A<X, T*, I> {};
具体例子:移除援用
#include <iostream>
template <typename T>
struct RemoveReference
{using type = T;};
template <typename T>
struct RemoveReference<T&>
{using type = T;};
template <typename T>
struct RemoveReference<T&&>
{using type = T;};
int main()
{
std::cout << std::boolalpha << std::is_reference<int&>::value << '\n';
std::cout << std::boolalpha << std::is_reference<int&&>::value << '\n';
std::cout << "---------------------\n";
std::cout << std::boolalpha << std::is_reference<RemoveReference<int>::type>::value << '\n';
std::cout << std::boolalpha << std::is_reference<RemoveReference<int&>::type>::value << '\n';
std::cout << std::boolalpha << std::is_reference<RemoveReference<int&&>::type>::value << '\n';
}
true
true
---------------------
false
false
false
3. 局部特化的成员
(1)成员的模板形参列表、模板实参列表必须和局部特化的形参列表、实参列表统一.
// primary template
template<class T, int I>
struct A {void f();
};
// primary template member definition
template<class T, int I>
void A<T, I>::f() {}
// partial specialization
template<class T>
struct A<T, 2> {void f();
void g();
void h();};
// member of partial specialization
template<class T>
void A<T, 2>::g() {}
(2)局部特化的成员也能够进一步显式特化.
// explicit (full) specialization of a member of partial specialization
template<>
void A<char, 2>::h() {}
(3)外围类模板的特化要优先于内层类模板的特化.
template<class T> struct A {
// primary member template
template<class T2>
struct B {};
// partial specialization of member template
template<class T2>
struct B<T2*> {};};
// full specialization of primary member template (will ignore the partial)
template<>
template<class T2>
struct A<short>::B {};
A<char>::B<int*> abcip; // uses partial specialization T2=int
A<short>::B<int*> absip; // uses full specialization of the primary (ignores partial)
A<char>::B<int> abci; // uses primary