看C++ Templates 16.1 Named Template Arguments
書中的例子實(shí)現(xiàn)手法使用多重/虛擬繼承, 實(shí)現(xiàn)手法感覺(jué)比較詭秘. 但是至少告訴我是可以實(shí)現(xiàn)的.
于是干脆自己也練了練手, 博君一笑. 只在VC7.1下測(cè)試過(guò), VC6也許可能可以迂回實(shí)現(xiàn), 但是估計(jì)工作量太大.
1. 首先需要一個(gè)基本的 If 語(yǔ)句.
template <bool, class T, class U>
struct if_
{
typedef T type;
};
template<class T, class U>
struct if_<false, T, U>
{
typedef U type;
};
2. 然后使用一個(gè) type_selector meta-function,
N表示第幾個(gè)默認(rèn)參數(shù)(注意我的默認(rèn)Policy參數(shù)DefaultPolicyArgs里面有一個(gè)meta data, 為0.
如果是用戶定義的Policy, 那么形如Policy2_is的模板類里面有一個(gè)meta data為2. 這個(gè)數(shù)字主要是用于定位.
最后的DefaultType是當(dāng)掃描一遍, 發(fā)現(xiàn)沒(méi)有任何對(duì)應(yīng)N位置的自定義Policy參數(shù), 那么就取這個(gè)為默認(rèn)值, 結(jié)束遞歸.(下面的4個(gè)void的特化版本就是干這個(gè)的)
template<
int N,
class T1,
class T2,
class T3,
class T4,
class DefaultType>
struct type_selector
{
typedef typename if_ <
(T1::value == N),
T1,
type_selector<N, T2, T3, T4, void, DefaultType> >::type eval_type;
typedef typename eval_type::type type;
};
//shift以后, 如果都是默認(rèn)值, 遞歸會(huì)來(lái)到這里, 結(jié)束.
template<
int N,
class DefaultType>
struct type_selector<N, void, void, void, void, DefaultType>
{
typedef DefaultType type;
};
struct DefaultPolicy1 {};
struct DefaultPolicy2 {};
struct DefaultPolicy3 {
public:
static void doPrint() {
std::cout << "DefaultPolicy3::doPrint()\n";
}
};
class DefaultPolicy4 {};
struct DefaultPolicyArgs {
static const int value = 0;
};
template <typename Policy>
struct Policy1_is
{
typedef Policy type;
static const int value = 1;
};
template <typename Policy>
struct Policy2_is
{
typedef Policy type;
static const int value = 2;
};
template <typename Policy>
struct Policy3_is
{
typedef Policy type;
static const int value = 3;
};
template <typename Policy>
struct Policy4_is
{
typedef Policy type;
static const int value = 4;
};
template<class T1, class T2, class T3, class T4>
struct PolicySelector
{
typedef typename type_selector<1, T1, T2, T3, T4, DefaultPolicy1>::type P1;
typedef typename type_selector<2, T1, T2, T3, T4, DefaultPolicy2>::type P2;
typedef typename type_selector<3, T1, T2, T3, T4, DefaultPolicy3>::type P3;
typedef typename type_selector<4, T1, T2, T3, T4, DefaultPolicy4>::type P4;
};
template <typename T1 = DefaultPolicyArgs,
typename T2 = DefaultPolicyArgs,
typename T3 = DefaultPolicyArgs,
typename T4 = DefaultPolicyArgs>
class BreadSlicer {
typedef typename PolicySelector<T1, T2, T3, T4> Policies;
public:
void print () {
std::cout << typeid(Policies::P3).name() << std::endl;
Policies::P3::doPrint();
}
void print_2()
{
std::cout << typeid(Policies::P2).name() << std::endl;
Policies::P2::print_2();
}
//...
};
//下面的就是測(cè)試代碼了.
class CustomPolicy {
public:
static void doPrint() {
std::cout << "CustomPolicy::doPrint()\n";
}
};
class CustomPolicy2 {
public:
static void print_2()
{
std::cout << "Here is CustomPolicy2 instance" << std::endl;
}
};
int main()
{
BreadSlicer<> bc1;
bc1.print();
BreadSlicer< Policy3_is<CustomPolicy>,
Policy2_is<CustomPolicy2> > bc2;
bc2.print();
return 0;
}
上面那個(gè)帖子的實(shí)現(xiàn)手法不太好, 當(dāng)client使用的時(shí)候, 還是需要
BreadSlicer< Policy3_is<CustomPolicy>, Policy2_is<CustomPolicy2> > bc;
復(fù)雜的嵌套模板語(yǔ)法, 如果能夠去掉PolicyN_is, 例如可以這樣
BreadSlicer<> bc2; //全部默認(rèn)policies
如果定制其中的policy2, 這樣
BreadSlicer< CustomPolicy2 > bc;
如果需要定制2, 3, 這樣
BreadSlicer< CustomPolicy2, CustomPolicy3 > bc;
與順序無(wú)關(guān), 先寫3, 再寫2也可以
BreadSlicer< CustomPolicy3, CustomPolicy2 > bc;
那就更加簡(jiǎn)單了.
幸運(yùn)的是, 這也是可以實(shí)現(xiàn)的, 而且與前面的帖子相比, 這個(gè)新的實(shí)現(xiàn)還直白簡(jiǎn)單, 使用起來(lái)由于直接使用Policy class作為參數(shù),
而無(wú)需通過(guò)PolicyN_is這樣的包裹, 使用起來(lái)也更加優(yōu)雅.
還是看看代碼:
1. 同上, 定義一個(gè)if語(yǔ)句.
template <bool, class T, class U>
struct if_
{
typedef typename T type;
};
template<class T, class U>
struct if_<false, T, U>
{
typedef typename U type;
};
2. 定義一個(gè)wrapper, 使得 type_wrapper<T>::type 有效(為T). 因?yàn)橹苯邮褂?T::type 可能遇到T根本沒(méi)有type這個(gè)typedef內(nèi)嵌類型.
template<class T>
struct type_wrapper
{
typedef T type;
};
struct DefaultPolicyArgs {
static const int value =
0;
//特殊meta-data, 為0表示默認(rèn)參數(shù)
};
3.
然后使用一個(gè) type_selector meta-function,
N表示第幾個(gè)默認(rèn)參數(shù)(注意我的默認(rèn)Policy參數(shù)DefaultPolicyArgs里面有一個(gè)meta data, 為0.
如果是用戶定義的Policy, 那么它也應(yīng)該定義一個(gè)meta data,.
這個(gè)數(shù)字主要是用于告訴selector它是想覆蓋第幾個(gè)默認(rèn)的policy參數(shù).
最后的DefaultType是當(dāng)掃描一遍, 發(fā)現(xiàn)沒(méi)有任何對(duì)應(yīng)N位置的自定義Policy參數(shù), 那么就取這個(gè)為默認(rèn)值, 結(jié)束遞歸.(下面的4個(gè)DefaultPolicyArgs的特化版本就是干這個(gè)的)
與前面的一個(gè)版本相比, 我不再使用void, 而是使用DefaultPolicyArgs來(lái)填充, 這樣在大部分情況下匹配速度要快. (指編譯速度)
template<
int N,
class T1,
class T2,
class T3,
class T4,
class DefaultType>
struct type_selector
{
typedef typename if_ <
(T1::value == N),
type_wrapper<T1>,
type_selector<N, T2, T3, T4, DefaultPolicyArgs, DefaultType> >::type eval_type;
typedef typename eval_type::type type;
};
//shift以后最終來(lái)到這里, 結(jié)束遞歸
template<
int N,
class DefaultType>
struct type_selector<N, DefaultPolicyArgs, DefaultPolicyArgs, DefaultPolicyArgs, DefaultPolicyArgs, DefaultType>
{
typedef DefaultType type;
};
struct DefaultPolicy1 {};
struct DefaultPolicy2 {};
struct DefaultPolicy3 {
public:
static void doPrint() {
std::cout << "DefaultPolicy3::doPrint()\n";
}
};
class DefaultPolicy4 {};
template<class T1, class T2, class T3, class T4>
struct PolicySelector
{
typedef typename type_selector<1, T1, T2, T3, T4, DefaultPolicy1>::type P1;
typedef typename type_selector<2, T1, T2, T3, T4, DefaultPolicy2>::type P2;
typedef typename type_selector<3, T1, T2, T3, T4, DefaultPolicy3>::type P3;
typedef typename type_selector<4, T1, T2, T3, T4, DefaultPolicy4>::type P4;
};
template <typename T1 = DefaultPolicyArgs,
typename T2 = DefaultPolicyArgs,
typename T3 = DefaultPolicyArgs,
typename T4 = DefaultPolicyArgs>
class BreadSlicer {
typedef typename PolicySelector<T1, T2, T3, T4> Policies;
public:
void print () {
std::cout << typeid(Policies::P3).name() << std::endl;
Policies::P3::doPrint();
}
void print_2()
{
std::cout << typeid(Policies::P2).name() << std::endl;
Policies::P2::print_2();
}
//...
};
class CustomPolicy2 {
public:
static const int value = 2; //關(guān)鍵的修改在此, 這個(gè)是實(shí)現(xiàn)定制Policy時(shí)需要提供的meta data
static void print_2()
{
std::cout << "Here is CustomPolicy2 instance" << std::endl;
}
};
class CustomPolicy3 {
public:
static const int value = 3; //meta data, 同上, 3表示這個(gè)代表的是用了替換Policy3
static void doPrint() {
std::cout << "CustomPolicy3::doPrint()\n";
}
};
//這樣, PolicyN_is 就沒(méi)有了, 唯一的要求就是, 當(dāng)實(shí)現(xiàn)custom policy的時(shí)候, 別忘了在其中定義一個(gè)叫做
value的整形常量, N代表替換哪個(gè)默認(rèn)的policy參數(shù).
int main()
{
BreadSlicer<>
bc1;
//全部默認(rèn)
bc1.print();
BreadSlicer< CustomPolicy2, CustomPolicy3 > bc2; //2,3定制, 是不是干凈一些?
bc2.print();
bc2.print_2();
std::cout << std::flush;
return 0;
}