最近看了C++11的一些特性,最感兴趣的是可变模板参数,自动类型推断和匿名函数。
Loki中的TypeList,是需要递归定义的,并且需要一个NullType作为尾节点。
可变模板参数使得实现TypeList更简洁,更易懂。
以下是我用C++11实现TypeList。
复制代码
//////////////////////////////////////////////////////////
  template
struct typelist
{
};
typedef typelist<> nulllist;
//////////////////////////////////////////////////////////
  template
  template
  struct length< typelist
{
enum { value = sizeof...(TList) };
};
//////////////////////////////////////////////////////////
  template
  template
  struct push_front< T, typelist
{
  typedef typelist
};
//////////////////////////////////////////////////////////
  template
  template
  struct pop_front< typelist
{
  typedef typelist
};
template<>
struct pop_front< nulllist >
{
typedef nulllist type;
};
//////////////////////////////////////////////////////////
  template
  template
  struct at< N, typelist
{
  typedef typename at< N-1, typelist
};
  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
  template
  struct indexof< T, typelist
{
  enum { value = IndexFixer
};
  template
  struct indexof< T, typelist
{
enum { value = 0 };
};
  template
struct indexof< T, nulllist >
{
enum { value = -1 };
};
//////////////////////////////////////////////////////////
  template
  template
  struct concat
{
  typedef typelist
};
  template
  struct concat
{
  typedef typelist
};
  template
  struct concat< T, typelist
{
  typedef typelist
};
//////////////////////////////////////////////////////////
  template
  template
  struct erase
{
  typedef typename concat
};
  template
  struct erase
{
  typedef typelist
};
  template
  struct erase
{
typedef nulllist type;
};
//////////////////////////////////////////////////////////
  template
  template
  struct erase_all
{
  typedef typename concat
};
  template
  struct erase_all
{
  typedef erase_all< T,typelist
};
  template
  struct erase_all
{
typedef U type;
};
  template
  struct erase_all
{
typedef nulllist type;
};
//////////////////////////////////////////////////////////
  template
  template
  struct no_duplicate< typelist
{
private:
  typedef typename no_duplicate< typelist
  typedef typename erase
public:
  typedef typename concat
};
template<>
struct no_duplicate< nulllist >
{
typedef nulllist type;
};
//////////////////////////////////////////////////////////
  template
  template
  struct replace
{
  typedef typename concat
};
  template
  struct replace
{
  typedef typename concat
};
  template
  struct replace
{
typedef nulllist type;
};
//////////////////////////////////////////////////////////
  template
  template
  struct replace_all
{
  typedef typename concat
};
  template
  struct replace_all
{
  typedef typename concat
};
  template
  struct replace_all
{
typedef nulllist type;
};
//////////////////////////////////////////////////////////
  template
  template
  struct reverse
{
  typedef typename concat
};
template<>
struct reverse< nulllist >
{
typedef nulllist type;
};
复制代码
例子:
复制代码
  #include 
  #include 
  #include "TypeList.h"#include 
using namespace hi::mpl;
int main()
{
  typedef typelist
  typedef typelist
  typedef typelist
typedef typelist<> elist;
  typedef typelist
typedef testlist mylist;
  std::cout << "length: " << length
bool b;
  b = std::is_same
std::cout << "is same: " << b << std::endl;
  b = std::is_same
std::cout << "is same: " << b << std::endl;
  std::cout << "indexof : " << indexof
  b = std::is_same
std::cout << "is same: " << b << std::endl;
  b = std::is_same< erase
std::cout << "is same: " << b << std::endl;
  b = std::is_same< no_duplicate
std::cout << "is same: " << b << std::endl;
  b = std::is_same< replace
std::cout << "is same: " << b << std::endl;
  b = std::is_same< replace_all
std::cout << "is same: " << b << std::endl;
  b = std::is_same< reverse
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;
}