XML|HTML|TXT
您当前位置: 软件开发>> 新利在线娱乐>> 软件开发行业资讯>> 浏览文章

使用c++11改写loki的TypeList

  最近看了C++11的一些特性,最感兴趣的是可变模板参数,自动类型推断和匿名函数。

  Loki中的TypeList,是需要递归定义的,并且需要一个NullType作为尾节点。

  可变模板参数使得实现TypeList更简洁,更易懂。

  以下是我用C++11实现TypeList。

  复制代码

  //////////////////////////////////////////////////////////

  template

  struct typelist

  {

  };

  typedef typelist<> nulllist;

  //////////////////////////////////////////////////////////

  template struct length;

  template

  struct length< typelist >

  {

  enum { value = sizeof...(TList) };

  };

  //////////////////////////////////////////////////////////

  template struct push_front;

  template

  struct push_front< T, typelist >

  {

  typedef typelist type;

  };

  //////////////////////////////////////////////////////////

  template struct pop_front;

  template

  struct pop_front< typelist >

  {

  typedef typelist type;

  };

  template<>

  struct pop_front< nulllist >

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template struct at;

  template

  struct at< N, typelist >

  {

  typedef typename at< N-1, typelist >::type type;

  };

  template

  struct at< 0, typelist >

  {

  typedef T type;

  };

  template<>

  struct at< 0, nulllist >

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template

  struct IndexFixer

  {

  enum { value = (A == B) ? B : A + 1 };

  };

  //////////////////////////////////////////////////////////

  template struct indexof;

  template

  struct indexof< T, typelist >

  {

  enum { value = IndexFixer>::value, -1>::value };

  };

  template

  struct indexof< T, typelist >

  {

  enum { value = 0 };

  };

  template

  struct indexof< T, nulllist >

  {

  enum { value = -1 };

  };

  //////////////////////////////////////////////////////////

  template struct concat;

  template

  struct concat, typelist >

  {

  typedef typelist type;

  };

  template

  struct concat, T >

  {

  typedef typelist type;

  };

  template

  struct concat< T, typelist >

  {

  typedef typelist type;

  };

  //////////////////////////////////////////////////////////

  template struct erase;

  template

  struct erase >

  {

  typedef typename concat >::type>::type type;

  };

  template

  struct erase >

  {

  typedef typelist type;

  };

  template

  struct erase

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template struct erase_all;

  template

  struct erase_all >

  {

  typedef typename concat >::type>::type type;

  };

  template

  struct erase_all >

  {

  typedef erase_all< T,typelist > type;

  };

  template

  struct erase_all >

  {

  typedef U type;

  };

  template

  struct erase_all

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template struct no_duplicate;

  template

  struct no_duplicate< typelist >

  {

  private:

  typedef typename no_duplicate< typelist >::type inner;

  typedef typename erase::type inner_result;

  public:

  typedef typename concat::type type;

  };

  template<>

  struct no_duplicate< nulllist >

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template struct replace;

  template

  struct replace >

  {

  typedef typename concat>::type>::type type;

  };

  template

  struct replace >

  {

  typedef typename concat >::type type;

  };

  template

  struct replace

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template struct replace_all;

  template

  struct replace_all >

  {

  typedef typename concat>::type>::type type;

  };

  template

  struct replace_all >

  {

  typedef typename concat>::type >::type type;

  };

  template

  struct replace_all

  {

  typedef nulllist type;

  };

  //////////////////////////////////////////////////////////

  template struct reverse;

  template

  struct reverse >

  {

  typedef typename concat>::type, T>::type type;

  };

  template<>

  struct reverse< nulllist >

  {

  typedef nulllist type;

  };

  复制代码

  例子:

  复制代码

  #include

  #include

  #include "TypeList.h"#include

  using namespace hi::mpl;

  int main()

  {

  typedef typelist testlist;

  typedef typelist tlist;

  typedef typelist hlist;

  typedef typelist<> elist;

  typedef typelist ilist;

  typedef testlist mylist;

  std::cout << "length: " << length::value << std::endl;

  bool b;

  b = std::is_same::type, bool>::value;

  std::cout << "is same: " << b << std::endl;

  b = std::is_same::type, ilist>::value;

  std::cout << "is same: " << b << std::endl;

  std::cout << "indexof : " << indexof::value << std::endl;

  b = std::is_same>::type, pop_front::type>::value ;

  std::cout << "is same: " << b << std::endl;

  b = std::is_same< erase::type, typelist>::value;

  std::cout << "is same: " << b << std::endl;

  b = std::is_same< no_duplicate::type, typelist>::value;

  std::cout << "is same: " << b << std::endl;

  b = std::is_same< replace::type, typelist>::value;

  std::cout << "is same: " << b << std::endl;

  b = std::is_same< replace_all::type, typelist>::value;

  std::cout << "is same: " << b << std::endl;

  b = std::is_same< reverse::type, typelist>::value;

  std::cout << "is same: " << b << std::endl;

  //std::cout << "is same: " << CompileTimeCount(int) << std::endl;

  //std::cout << "is same: " << CompileTimeCount(int) << std::endl;

  return 0;

  }


手机:18678812288 E-Mail:1069706080@qq.com
地址:山东省济南市舜耕路泉城公园东门园内向北50米 鲁ICP备07011972号 版权所有2008-2013 新利体育18
Baidu