Boost :: mpl type function application

I have a function that I want to execute for all types in a type list (currently represented by a list of mpl --- is that even a reasonable way to approach it?)

The key here is that a function only cares about the type, not the actual data; it calls a static function in this type to get some information, and then translates it into a hash table for later reference.

However, as far as I can tell, mpl has no means for this - the closest I can find is the mpl for_each operator, but it seems to want to be used for actual instances of each of the types, not the types themselves.

There was a “apply” function in the Loki library, which is more or less what I'm looking for - it circumvented the problem of creating an instance by passing a pointer to a type in the type list as a parameter that will help with subtraction, but without making a full instance. What should I look in MPL to get this functionality? Or am I missing something obvious?

+7
source share
3 answers

You can use for_each "overload" with TransformOp to avoid creating type instances:

struct functor { template<class T> void operator()( mpl::identity<T> ) { std::cout << typeid(T).name() << '\n'; } }; mpl::for_each<types, mpl::make_identity<_> >( functor() ); 
+8
source

The simplest option is simply:

 #include <boost/mpl/vector.hpp> #include <boost/mpl/transform.hpp> #include <boost/type_traits/add_pointer.hpp> #include <boost/mpl/placeholders.hpp> #include <boost/mpl/for_each.hpp> #include <typeinfo> #include <iostream> struct functor{ template<class T> void operator()(T*){ std::cout << typeid(T).name() << '\n'; } }; int main(){ namespace mpl = boost::mpl; using namespace mpl::placeholders; typedef mpl::vector<char, int, float, bool> typelist; typedef mpl::transform<typelist, boost::add_pointer<_1>>::type ptypelist; mpl::for_each<ptypelist>(functor()); } 
+3
source

Do the same in MPL: call boost::mpl::transform with boost::add_pointer to make a sequence of pointers to your types, and then use boost::mpl::for_each .

0
source

All Articles