From: Douglas Gregor Date: Tue, 9 Jun 2009 21:22:32 +0000 (+0000) Subject: Example metaprogram for reversing and searching in a type list X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6d968e89411406f0fd22ae69c77c79a425e2de6f;p=clang Example metaprogram for reversing and searching in a type list git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73147 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/SemaTemplate/example-typelist.cpp b/test/SemaTemplate/example-typelist.cpp index e130725530..4a2aeb20e7 100644 --- a/test/SemaTemplate/example-typelist.cpp +++ b/test/SemaTemplate/example-typelist.cpp @@ -1,5 +1,6 @@ // RUN: clang-cc -fsyntax-only -verify %s +// A simple cons-style typelist struct nil { }; template @@ -8,6 +9,17 @@ struct cons { typedef Tail tail; }; +// is_same trait, for testing +template +struct is_same { + static const bool value = false; +}; + +template +struct is_same { + static const bool value = true; +}; + // metaprogram that computes the length of a list template struct length; @@ -26,3 +38,61 @@ typedef cons > > > unsigned_inttypes; int length0[length::value == 4? 1 : -1]; + +// metaprogram that reverses a list + +// FIXME: I would prefer that this be a partial specialization, but +// that requires partial ordering of class template partial +// specializations. +template +class reverse { + typedef typename reverse::type reversed_tail; + + typedef typename reverse::type most_of_tail; + +public: + typedef cons >::type> type; +}; + +template +class reverse > { +public: + typedef cons type; +}; + +template<> +class reverse { +public: + typedef nil type; +}; + +int reverse0[is_same::type, + cons > > > >::value? 1 : -1]; + +// metaprogram that finds a type within a list + +// FIXME: I would prefer that this be a partial specialization, but +// that requires partial ordering of class template partial +// specializations. +template +struct find : find { }; + +template +struct find, T> { + typedef cons type; +}; + +template +struct find { + typedef nil type; +}; + +int find0[is_same::type, + cons > >::value? + 1 : -1]; +int find1[is_same::type, nil>::value? 1 : -1]; +