VC6不支持typedef中递归。终于找到了Tuple的实现方案。

众所周知,VC6对Templates的支持非常差,但是由于工作需要,我还是希望能够在VC6上实现一个Tuple,这个好像比实现remove_reference还难了(boost好像也没有实现一个VC6下可运行的Tuple),因为Tuple的实现中用到typedef的递归,这种方式VC6会出现死循环。我做了一个简化的测试工程,请高手帮忙看看有没有好的办法绕过这个问题?多谢!
代码如下:(如果用enum部分进行递归,VC6下能正确运行)
InBlock.gif#include <iostream.h>
InBlock.gif
InBlock.giftemplate <typename T, int n>
InBlock.gif struct Get
InBlock.gif{
InBlock.gif  template < int unused>
InBlock.gif   struct GetInner
InBlock.gif  {
InBlock.gif     //enum { Value = Get<T, n+1>::Value};
InBlock.gif    typedef Get<T, n+1>::Type Type;
InBlock.gif  };
InBlock.gif
InBlock.gif  template <>
InBlock.gif   struct GetInner<2>
InBlock.gif  {
InBlock.gif     //enum { Value = 100};
InBlock.gif    typedef int Type;
InBlock.gif  };
InBlock.gif
InBlock.gif   //enum { Value = GetInner<n>::Value};
InBlock.gif  typedef GetInner<n>::Type Type;
InBlock.gif};
InBlock.gif
InBlock.gif int main()
InBlock.gif{
InBlock.gif   //cout << Get<int, 0>::Value <<endl;
InBlock.gif  typedef Get< int, 0>::Type TmpType;
InBlock.gif  cout << typeid(TmpType).name() <<endl;
InBlock.gif   return 0;
InBlock.gif}
 
借鉴了boost的一段代码,解决了VC6下实现Tuple的问题:
注释掉的部分是原来有问题的实现,为何boost的解决方案可行,还没来得及仔细研究,如果您知道,请告诉我:)
 
InBlock.gif   /*
InBlock.gif  template<
InBlock.gif    typename Typelist,    
InBlock.gif    int Index, //requested element index
InBlock.gif    int Step = 0, //current recusion step
InBlock.gif    bool Stop=(Index==Step), //stop recusion flag
InBlock.gif    bool OutOfRange = Length<Typelist>::Value==0 //out of range flag
InBlock.gif    >
InBlock.gif    struct Get
InBlock.gif    {
InBlock.gif    template<bool unused1, bool unused2>
InBlock.gif    struct GetInner
InBlock.gif    {
InBlock.gif      typedef typename Get<typename Typelist::tail, Index, Step+1>::Type Type;
InBlock.gif    };
InBlock.gif    
InBlock.gif    // found
InBlock.gif    template<>
InBlock.gif      struct GetInner<true, false>
InBlock.gif    {
InBlock.gif      typedef typename Typelist::head Type;    
InBlock.gif    };    
InBlock.gif    // found
InBlock.gif    template<>
InBlock.gif      struct GetInner<true, true>
InBlock.gif    {
InBlock.gif      typedef typename Typelist::head Type;        
InBlock.gif    };
InBlock.gif    template<>
InBlock.gif      struct GetInner<false, true>
InBlock.gif    {
InBlock.gif      //if OutOfRange is 'true' the 'type' is undefined
InBlock.gif      //so we'll get a compile-time error
InBlock.gif    };
InBlock.gif
InBlock.gif    typedef GetInner<Stop, OutOfRange>::Type Type;
InBlock.gif
InBlock.gif  };
InBlock.gif  */

InBlock.gif    
InBlock.gif   // Workaround the lack of partial specialization in some compilers
InBlock.gif  template< int N>
InBlock.gif     struct _element_type
InBlock.gif  {
InBlock.gif                template<typename Tuplelist>
InBlock.gif       struct inner
InBlock.gif                {
InBlock.gif                 private:
InBlock.gif      typedef typename Tuplelist::tail tail;
InBlock.gif      typedef _element_type<N-1> next_elt_type;
InBlock.gif        
InBlock.gif                 public:
InBlock.gif      typedef typename _element_type<N-1>::template inner<tail>::RET RET;
InBlock.gif                };
InBlock.gif  };
InBlock.gif    
InBlock.gif  template<>
InBlock.gif     struct _element_type<0>
InBlock.gif  {
InBlock.gif                template<typename Tuplelist>
InBlock.gif       struct inner
InBlock.gif                {
InBlock.gif      typedef typename Tuplelist::head RET;
InBlock.gif                };
InBlock.gif  };
InBlock.gif
InBlock.gif   // Return the Nth type of the given Tuple
InBlock.gif  template<typename Tuplelist, int N>
InBlock.gif   struct Get
InBlock.gif  {
InBlock.gif       private:
InBlock.gif        typedef _element_type<N> nth_type;
InBlock.gif        
InBlock.gif       public:
InBlock.gif        typedef typename nth_type::template inner<Tuplelist>::RET RET;
InBlock.gif        typedef RET Type;
InBlock.gif  };
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值