Simple MPL question: Generate code from type sequence
I would like to generate one call to a templated function for each type
in an MPL sequence. Which is the best way to do this?
E.g.:
typedef vector
Johan,
there is an mpl::for_each construct to do this. Unfortunately for_each is not documented, but it
is a part of mpl library. If you have the MPL book from Dave and Alexey you can read about it on
page 175. On page 176-179 you will find 3 differnt examples about type printing. Below is one of
the approaches converted to you example:
Your example should work with the following sample:
#include <iostreams>
#include
I would like to generate one call to a templated function for each type in an MPL sequence. Which is the best way to do this? E.g.:
typedef vector
SomeVector; template <typename T> write_size() { std::cout << sizeof(T) << " "; } Should somehow expand to:
write_size<float>(); write_size<double>(); write_size<long double>();
I have managed to do this "manually" in a fairly general way, what I'm looking for is some part of boost which replaces my ForEach struct below or some nicer way of writing the same code:
============================================== struct WriteSize { template<typename T> void Call() {write_size<T>();} };
template
struct ForEach { template <class Func> static void Call(Func& func) { func.Call<typename It::type>(); ForEach ::Call(func); } }; template <class End> struct ForEach
{ template <class Func> static void Call(Func&) {} }; // Then I can generate the calls by WriteSize ws; ForEach
::type, end ::type>::Call(ws); ==============================================
-- System Developer TouchTable AB Tel: +46 (0)31 773 68 12 johan.torp@touchtable.se _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Johan Torp
-
Ovanes Markarian