zoukankan      html  css  js  c++  java
  • boost一个编译时计数类的实现(__if_exists关键字)

    使用在BOOST_TYPEOF宏中.

    # if BOOST_WORKAROUND(BOOST_MSVC,>=1300) && defined(_MSC_EXTENSIONS)
            template<int N> struct the_counter;
    
            template<typename T,int N = 5/*for similarity*/>
            struct encode_counter
            {
                __if_exists(the_counter<N + 256>)
                {
                    BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 257>::count));
                }
                __if_not_exists(the_counter<N + 256>)
                {
                    __if_exists(the_counter<N + 64>)
                    {
                        BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 65>::count));
                    }
                    __if_not_exists(the_counter<N + 64>)
                    {
                        __if_exists(the_counter<N + 16>)
                        {
                            BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 17>::count));
                        }
                        __if_not_exists(the_counter<N + 16>)
                        {
                            __if_exists(the_counter<N + 4>)
                            {
                                BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 5>::count));
                            }
                            __if_not_exists(the_counter<N + 4>)
                            {
                                __if_exists(the_counter<N>)
                                {
                                    BOOST_STATIC_CONSTANT(unsigned,count=(encode_counter<T,N + 1>::count));
                                }
                                __if_not_exists(the_counter<N>)
                                {
                                    BOOST_STATIC_CONSTANT(unsigned,count=N);
                                    typedef the_counter<N> type;
                                }
                            }
                        }
                    }
                }
            };
    
    # define BOOST_TYPEOF_INDEX(T) (encode_counter<T>::count)
    # define BOOST_TYPEOF_NEXT_INDEX(next)
    # else
            template<int N> struct encode_counter : encode_counter<N - 1> {};
            template<> struct encode_counter<0> {};
    
            //Need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int)==4)
            char (*encode_index(...))[5];
    
    # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
    # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
    # endif

     笔记:

      保存全局的类型列表(type list).在mirror库里使用另一种方法(使用__counter__关键字,具体实现有待深入),能够在一个类定义时,将该类保存到一个能够索引的地方.即使是模板编程,也需要在定义时确定类型或者得到类型信息.比如定义一个tuple, 在定义时你需要类似:typedef boost::mpl::vector<int, float, double> tuple;既是tuple在被定义时,int,float,double3个类型是准备好了(即使是前向声明).而如果你想在以后扩充,比如加入一个char类型, typedef boost::mpl::vector<int, float, double,char> tuple;是不可能的,这样会重复定义,duple无论如何是不能扩充一个新类型的.可是如果存在一个方法手段,每当我们在新的代码位置定义一个新类型时,都能把这个类型保存到一个"统一"的地方供我们索引,无疑是很有用的.比如设计一个rtti系统,在编译时阶段,获取到类型相关信息就很吸引人.

  • 相关阅读:
    微信公众平台消息接口开发(32)空气质量指数查询
    微信公众平台消息接口开发(33)智能快递查询
    微信公众平台消息接口开发(34)桃花运测试
    微信5.0体验贴:即将横“扫”一切
    微信官方首次表态:微信公众平台不是营销工具
    微信公众平台消息接口开发(34)微信墙
    微信 5.0 内部测试版下载地址
    PHP和MySQL Web开发 经典书籍
    字符数组在C++、C#等语言中的操作
    CF555B 贪心
  • 原文地址:https://www.cnblogs.com/flytrace/p/2877457.html
Copyright © 2011-2022 走看看