看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; }
|