Boost logo

Boost Users :

From: Johan Torp (johan.torp_at_[hidden])
Date: 2007-01-04 05:34:14


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<float,double,long double> 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 <class It, class End> struct ForEach
{
  template <class Func> static void Call(Func& func)
  {
    func.Call<typename It::type>();
    ForEach<typename next<It>::type, End>::Call(func);
  }
};

template <class End> struct ForEach<End, End>
{
  template <class Func> static void Call(Func&) {}
};

// Then I can generate the calls by
WriteSize ws;
ForEach<begin<SomeVector;>::type, end<SomeVector;>::type>::Call(ws);

==============================================

-- 
System Developer
TouchTable AB
Tel: +46 (0)31 773 68 12
johan.torp_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net