锘?!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
銆銆鏈榪戠湅浜咰++11鐨勪竴浜涚壒鎬э紝鏈鎰熷叴瓚g殑鏄彲鍙樻ā鏉垮弬鏁幫紝鑷姩綾誨瀷鎺ㄦ柇鍜屽尶鍚嶅嚱鏁般?/span>
銆銆Loki涓殑TypeList錛屾槸闇瑕侀掑綊瀹氫箟鐨勶紝騫朵笖闇瑕佷竴涓狽ullType浣滀負灝捐妭鐐廣?/span>
銆銆鍙彉妯℃澘鍙傛暟浣垮緱瀹炵幇TypeList鏇寸畝媧侊紝鏇存槗鎳傘?/span>
銆銆浠ヤ笅鏄垜鐢–++11瀹炵幇TypeList銆?/span>
銆銆澶嶅埗浠g爜
銆銆//////////////////////////////////////////////////////////
銆銆template<typename... TList>
銆銆struct typelist
銆銆{
銆銆};
銆銆typedef typelist<> nulllist;
銆銆//////////////////////////////////////////////////////////
銆銆template<typename... TList> struct length;
銆銆template<typename... TList>
銆銆struct length< typelist<TList...> >
銆銆{
銆銆enum { value = sizeof...(TList) };
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename T, typename... TList> struct push_front;
銆銆template<typename T, typename... TList>
銆銆struct push_front< T, typelist<TList...> >
銆銆{
銆銆typedef typelist<T, TList...> type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename... TList> struct pop_front;
銆銆template<typename T, typename... TList>
銆銆struct pop_front< typelist<T, TList...> >
銆銆{
銆銆typedef typelist<TList...> type;
銆銆};
銆銆template<>
銆銆struct pop_front< nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<unsigned int N, typename... TList> struct at;
銆銆template<unsigned int N, typename T, typename... TList>
銆銆struct at< N, typelist<T, TList...> >
銆銆{
銆銆typedef typename at< N-1, typelist<TList...> >::type type;
銆銆};
銆銆template<typename T, typename... TList>
銆銆struct at< 0, typelist<T, TList...> >
銆銆{
銆銆typedef T type;
銆銆};
銆銆template<>
銆銆struct at< 0, nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<int A, int B>
銆銆struct IndexFixer
銆銆{
銆銆enum { value = (A == B) ? B : A + 1 };
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename T, typename... TList> struct indexof;
銆銆template<typename T, typename H, typename... TList>
銆銆struct indexof< T, typelist<H, TList...> >
銆銆{
銆銆enum { value = IndexFixer<indexof<T, typelist<TList...>>::value, -1>::value };
銆銆};
銆銆template<typename T, typename... TList>
銆銆struct indexof< T, typelist<T, TList...> >
銆銆{
銆銆enum { value = 0 };
銆銆};
銆銆template<typename T>
銆銆struct indexof< T, nulllist >
銆銆{
銆銆enum { value = -1 };
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename A, typename B> struct concat;
銆銆template<typename... A, typename... B>
銆銆struct concat<typelist<A...>, typelist<B...> >
銆銆{
銆銆typedef typelist<A..., B...> type;
銆銆};
銆銆template<typename T, typename... TList>
銆銆struct concat<typelist<TList...>, T >
銆銆{
銆銆typedef typelist<TList..., T> type;
銆銆};
銆銆template<typename T, typename... TList>
銆銆struct concat< T, typelist<TList...> >
銆銆{
銆銆typedef typelist<T, TList...> type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename T, typename... TList> struct erase;
銆銆template<typename T, typename H, typename... TList>
銆銆struct erase<T, typelist<H, TList...> >
銆銆{
銆銆typedef typename concat<H, typename erase< T, typelist<TList...> >::type>::type type;
銆銆};
銆銆template<typename T, typename... TList>
銆銆struct erase<T, typelist<T, TList...> >
銆銆{
銆銆typedef typelist<TList...> type;
銆銆};
銆銆template<typename T>
銆銆struct erase<T, nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename T, typename... TList> struct erase_all;
銆銆template<typename T, typename H, typename... TList>
銆銆struct erase_all<T, typelist<H, TList...> >
銆銆{
銆銆typedef typename concat<H, typename erase_all< T, typelist<TList...> >::type>::type type;
銆銆};
銆銆template<typename T, typename... TList>
銆銆struct erase_all<T, typelist<T, TList...> >
銆銆{
銆銆typedef erase_all< T,typelist<TList...> > type;
銆銆};
銆銆template<typename T, typename U>
銆銆struct erase_all<T, typelist<U> >
銆銆{
銆銆typedef U type;
銆銆};
銆銆template<typename T>
銆銆struct erase_all<T, nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename T, typename...TList> struct no_duplicate;
銆銆template<typename T, typename...TList>
銆銆struct no_duplicate< typelist<T, TList...> >
銆銆{
銆銆private:
銆銆typedef typename no_duplicate< typelist<TList...> >::type inner;
銆銆typedef typename erase<T, inner>::type inner_result;
銆銆public:
銆銆typedef typename concat<T, inner_result>::type type;
銆銆};
銆銆template<>
銆銆struct no_duplicate< nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename R, typename T, typename...TList> struct replace;
銆銆template<typename R, typename T, typename H, typename...TList>
銆銆struct replace<R, T, typelist<H, TList...> >
銆銆{
銆銆typedef typename concat<H, typename replace<R, T, typelist<TList...>>::type>::type type;
銆銆};
銆銆template<typename R, typename H, typename...TList>
銆銆struct replace<R, H, typelist<H, TList...> >
銆銆{
銆銆typedef typename concat<R, typelist<TList...> >::type type;
銆銆};
銆銆template<typename R, typename T>
銆銆struct replace<R, T, nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename R, typename T, typename...TList> struct replace_all;
銆銆template<typename R, typename T, typename H, typename...TList>
銆銆struct replace_all<R, T, typelist<H, TList...> >
銆銆{
銆銆typedef typename concat<H, typename replace_all<R, T, typelist<TList...>>::type>::type type;
銆銆};
銆銆template<typename R, typename H, typename...TList>
銆銆struct replace_all<R, H, typelist<H, TList...> >
銆銆{
銆銆typedef typename concat<R, typename replace_all<R, H, typelist<TList...>>::type >::type type;
銆銆};
銆銆template<typename R, typename T>
銆銆struct replace_all<R, T, nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆//////////////////////////////////////////////////////////
銆銆template<typename T, typename...TList> struct reverse;
銆銆template<typename T, typename...TList>
銆銆struct reverse<typelist<T, TList...> >
銆銆{
銆銆typedef typename concat<typename reverse<typelist<TList...>>::type, T>::type type;
銆銆};
銆銆template<>
銆銆struct reverse< nulllist >
銆銆{
銆銆typedef nulllist type;
銆銆};
銆銆澶嶅埗浠g爜
銆銆渚嬪瓙錛?/span>
銆銆澶嶅埗浠g爜
銆銆#include <iostream>
銆銆#include <vector>
銆銆#include "TypeList.h"#include <type_traits>
銆銆using namespace hi::mpl;
銆銆int main()
銆銆{
銆銆typedef typelist<int, float, bool, float> testlist;
銆銆typedef typelist<float, bool> tlist;
銆銆typedef typelist<int, float> hlist;
銆銆typedef typelist<> elist;
銆銆typedef typelist<int> ilist;
銆銆typedef testlist mylist;
銆銆std::cout << "length: " << length<mylist>::value << std::endl;
銆銆bool b;
銆銆b = std::is_same<at<2, mylist>::type, bool>::value;
銆銆std::cout << "is same: " << b << std::endl;
銆銆b = std::is_same<push_front<int, elist>::type, ilist>::value;
銆銆std::cout << "is same: " << b << std::endl;
銆銆std::cout << "indexof : " << indexof<bool, mylist>::value << std::endl;
銆銆b = std::is_same<pop_front<typelist<>>::type, pop_front<ilist>::type>::value ;
銆銆std::cout << "is same: " << b << std::endl;
銆銆b = std::is_same< erase<bool, mylist>::type, typelist<int, float, float>>::value;
銆銆std::cout << "is same: " << b << std::endl;
銆銆b = std::is_same< no_duplicate<mylist>::type, typelist<int, float, bool>>::value;
銆銆std::cout << "is same: " << b << std::endl;
銆銆b = std::is_same< replace<double, bool, mylist>::type, typelist<int, float, double, float>>::value;
銆銆std::cout << "is same: " << b << std::endl;
銆銆b = std::is_same< replace_all<double, float, mylist>::type, typelist<int, double, bool, double>>::value;
銆銆std::cout << "is same: " << b << std::endl;
銆銆b = std::is_same< reverse<mylist>::type, typelist<float, bool, float, int>>::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;
銆銆}