7) 占位符类型placeHolder [原创,泛型编程,自由下载转载,需注明出处]

要泛型设计中,某些类型或者值在编译阶段是无法确实的,但是又确实需要把这个位子给占住,在实际运行中,再用确切的类型/值来替换。

就好比一个人雇人排队买房:排队的是占位符,但是真正买房时,还是你自己去的。

下面实现了占位符功能,且最多支持5个占位符。


#ifndef placeHolder_h__
#define placeHolder_h__
#include <mplconfig.h>


#if HAS_BOOST_SUPPORT==0


namespace MiniMPL
{
    template<int N> struct Args;


    template<> struct Args<-1> {};
    typedef Args<-1>    _;


    template<> struct Args<0> {};
    typedef Args<0>     _0;


    template<> struct Args<1>
    {
        template<typename A1,typename A2=NullType,typename A3=NullType,typename A4=NullType,typename A5=NullType>
        struct apply
        {
            typedef A1 type;
        };
    };


    typedef Args<1>  _1;


    template<> struct Args<2>
    {
        template<typename A1,typename A2,typename A3=NullType,typename A4=NullType,typename A5=NullType>
        struct apply
        {
            typedef A2 type;
        };


    };


    typedef Args<2>     _2;  


    template<> struct Args<3>
    {
        template<typename A1,typename A2,typename A3,typename A4=NullType,typename A5=NullType>
        struct apply
        {
            typedef A3 type;
        };


    };


    typedef Args<3>     _3; 


    template<> struct Args<4>
    {
        template<typename A1,typename A2,typename A3,typename A4,typename A5=NullType>
        struct apply
        {
            typedef A4 type;
        };


    };


    typedef Args<4>     _4; 


    template<> struct Args<5>
    {
        template<typename A1,typename A2,typename A3,typename A4,typename A5>
        struct apply
        {
            typedef A5 type;
        };


    };


    typedef Args<5>     _5; 
}


#endif   //end of HAS_BOOST_SUPPORT


#endif // placeHolder_h__

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值