- 论坛徽章:
- 0
|
既然楼主喜欢折腾,我们就来折腾折腾~~
VS2010 和 g++ 4.3.4 编译通过。- #include <climits>
- #include <iostream>
- #include <typeinfo>
- using namespace std;
- struct NullType {};
- template <typename First, typename Rest>
- struct TypeList
- {
- typedef First first;
- typedef Rest rest;
- };
- template <typename List1, typename List2>
- struct AppendList
- {
- typedef TypeList<typename List1::first, typename AppendList<typename List1::rest, List2>::type > type;
- };
- template <typename List2>
- struct AppendList<NullType, List2>
- {
- typedef List2 type;
- };
- template <typename List, template <typename T> class Predicate, bool ok = Predicate<typename List::first>::yes>
- struct FindIf;
- template <template <typename T> class Predicate, bool ok>
- struct FindIf<NullType, Predicate, ok>
- {
- typedef NullType type;
- };
- template <typename List, template <typename T> class Predicate>
- struct FindIf<List, Predicate, true>
- {
- typedef typename List::first type;
- };
- template <typename List, template <typename T> class Predicate>
- struct FindIf<List, Predicate, false>
- {
- typedef typename FindIf<typename List::rest, Predicate>::type type;
- };
- typedef TypeList<signed char,
- TypeList<signed short int,
- TypeList<signed int,
- TypeList<signed long int,
- #ifdef LLONG_MAX
- TypeList<signed long long int,
- #endif
- NullType>
- #ifdef LLONG_MAX
- >
- #endif
- > > > SignedTypes;
- typedef TypeList<unsigned char,
- TypeList<unsigned short int,
- TypeList<unsigned int,
- TypeList<unsigned long int,
- #ifdef ULLONG_MAX
- TypeList<unsigned long long int,
- #endif
- NullType>
- #ifdef ULLONG_MAX
- >
- #endif
- > > > UnsignedTypes;
- typedef AppendList<SignedTypes, UnsignedTypes>::type IntTypes;
- template <size_t size>
- struct SizePredicate
- {
- template <typename T>
- struct predicate
- {
- static const bool yes = (sizeof(T) == size);
- };
- };
- int main()
- {
- cout<<typeid(FindIf<IntTypes, SizePredicate<4>::predicate>::type).name()<<endl;
- return 0;
- }
复制代码 |
|