Say you have a tuple and want to generate a new tuple by applying a metafunction on each type of the first one. What' the most efficient C++ metafuntion to accomplish to this task? Is it also possible to use C++0x variadic template to provide a better implementation?
+6
A:
How 'bout this one:
template<typename Metafun, typename Tuple>
struct mod;
// using a meta-function class
template<typename Metafun, template<typename...> class Tuple, typename ...Types>
struct mod<Metafun, Tuple<Types...>> {
typedef Tuple<typename Metafun::template apply<Types>::type...>
type;
};
Then
typedef std::tuple<int, bool> tuple_foo;
struct add_pointer {
template<typename T>
struct apply { typedef T *type; };
};
typedef mod<add_pointer, tuple_foo>::type tuple_ptrfoo;
That's using a metafunction class by wrapping the apply
into a non-template. That allows passing it to C++03 templates (which cannot accept templates with arbitrary parameters by simply doing template<typename...> class X
). You may, of course, accept a pure metafunction (not a class) too
template<template<typename...> class Metafun, typename Tuple>
struct mod;
// using a meta-function
template<template<typename...> class Metafun, template<typename...> class Tuple,
typename ...Types>
struct mod<Metafun, Tuple<Types...>> {
typedef Tuple<typename Metafun<Types>::type...>
type;
};
And use the std::add_pointer
template
typedef mod<std::add_pointer, tuple_foo>::type tuple_ptrfoo;
Or you can wrap it into a class so it is compatible with the first version
// transforming a meta function into a meta function class
template<template<typename...> class Metafun>
struct ToClass {
template<typename ... T>
struct apply { typedef Metafun<T...> type; };
};
typedef mod<ToClass<std::add_pointer>, tuple_foo>::type tuple_ptrfoo;
Hope it helps.
Johannes Schaub - litb
2010-03-17 08:02:16
Cool litb (awgn).
Nicola Bonelli
2010-03-17 08:41:29