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