The Boost.MPL is a great tool for template meta-programming. It basically contains an entire compile-time implementation of the STL containers and algorithms. So, yes, there are plenty of alternatives to loki::Typelist. In MPL terms, these are Sequences . You can choose, like with STL containers, whether you want random-access or not, associative container or not, sorted or not, etc. It goes quite a bit beyond the Typelist implementation.
If you are reading Alexandrescu's book, the important thing is to learn the tricks of the trade, in template meta-programming. For actual feature-rich implementations and code that you can use, you should look towards Boost, the Boost.MPL in particular, and, of course, David Abraham's book on template meta-programming which, besides being a really good book on TMP, is almost an instruction manual to using the Boost.MPL.
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
In most cases where typelists were used in old C++ (for example in tuple<>) , we do not need them any more - variadic templates would do the job better, and more efficiently too. As such, IMHO, libraries like Boost MPL would be rarely used; most of the verbose and somewhat intractable code in these libraries would eventually become thin wrappers over variadic parameter packs any way.
For example,
#include <tuple>
template< typename... TYPES > struct typelist {};
template< typename... TYPES > struct size {} ;
template< typename... TYPES > struct size< typelist<TYPES...> >
{ enum { value = sizeof...(TYPES) } ; } ;
template< typename T, typename... TYPES > struct push_front {} ;
template< typename T, typename... TYPES > struct push_front< T, typelist<TYPES...> >
{ typedef typelist< T, TYPES... > type ; } ;
template< typename... TYPES > struct pop_front {} ;
template< typename T, typename... TYPES > struct pop_front< typelist< T, TYPES... > >
{ typedef typelist<TYPES...> type ; } ;
template< unsigned int N, typename... TYPES > struct at {} ;
template< unsigned int N, typename... TYPES > struct at< N, typelist<TYPES...> >
{ typedef typename std::tuple_element< N, std::tuple<TYPES...> >::type type ; };
// .. etc
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Or maybe this:
template <class Tuple, class T, std::size_t Index = 0>
struct find_first;
template <class T, std::size_t Index>
struct find_first< std::tuple<>, T, Index>
{
static_assert(false, "Type not found in find_first");
};
template <class Head, class... Rest, class T, std::size_t Index>
struct find_first< std::tuple<Head, Rest...>, T, Index> :
public find_first< std::tuple<Rest...>, T, Index + 1> { };
template <class Head, class... Rest, std::size_t Index>
struct find_first< std::tuple<Head, Rest...>, Head, Index>
: public boost::mpl::size_t<Index> { };
void test_variadic_IndexOf()
{
typedef std::tuple<char, int, short> T;
std::cout << find_first<T, short>::value << '\n';
}
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
> I found a solution from
> htp://stackoverflow.com/questions/6032089/position-of-a-type-in-a-variadic-template-parameter-pack
Needlessly over-elaborate, isn't it? This would suffice:
template< typename T, typename U, int N = 0 > struct indexof ;
template< typename T, int N > struct indexof< T, typelist<>, N > { enum { value = -1 } ; };
template< typename T, typename FIRST, typename ...REST, int N >
struct indexof< T, typelist<FIRST,REST...>, N >
: std::conditional< std::is_same<T,FIRST>::value,
std::integral_constant<int,N>,
indexof< T, typelist<REST...>, N+1 > >::type {} ;
All that we need to do is recursively unpack the parameters one by one, keeping a count of how many have been unpacked so far, and ending the recursion if the type has been found (or the last parameter has been unpacked).
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
>>what else could we learn from TMP?
TMP is a form of pure functional programming (no mutable data, no side-effects). So, in some sense you learn, from TMP, a very fundamental form of programming. But, academics aside, what's more important with TMP is what you can learn to use it for. Expression templates come to mind, among other things like concept-maps or all sorts of applications of tuples (or typelists).
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457