Impossible is nothing  
  爱过知情重醉过知酒浓   花开花谢终是空   缘份不停留像春风来又走   女人如花花似梦
公告
日历
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456
统计
  • 随笔 - 8
  • 文章 - 91
  • 评论 - 16
  • 引用 - 0

导航

常用链接

留言簿(4)

随笔分类(4)

随笔档案(8)

文章分类(77)

文章档案(91)

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

看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;
}
posted on 2006-02-27 23:14 笑笑生 阅读(321) 评论(0)  编辑 收藏 引用 所属分类: C++语言

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 笑笑生 Powered by: 博客园 模板提供:沪江博客