C++模板—部分特化
Posted gzming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++模板—部分特化相关的知识,希望对你有一定的参考价值。
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
以上是关于C++模板—部分特化的主要内容,如果未能解决你的问题,请参考以下文章