Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2006-08-14 13:50:41


<imre_at_[hidden]> writes:

> Hi,
>
> Is it possible to do this trick during compilation time?
> I was able to go from type lists to boost::cons but I can't find a way to
> make that last step.

Not sure what you mean by "that last step..."

  namespace mpl = boost::mpl;
  using namespace mpl::placeholders;

  typedef mpl::vector<int, long, char const*> types;
  typedef mpl::fold<types, boost::cons<_2,_1> >::type tuple_type;
  tuple_type some_tuple;

If you want it to be a specialization of the boost::tuple template
(misguided, IMO) you can do it, but not "algorithmically." You need a
bunch of partial specializations:

      template <class V, std::size_t n = mpl::size<V>::type::value >
      struct types_to_tuple;

      
      template <class V>
      struct types_to_tuple<V, 0>
      {
          typedef boost::tuple<> type;
      }

      template <class V>
      struct types_to_tuple<V, 1>
      {
          typedef boost::tuple<typename mpl::at_n<0>::type> type;
      }

      template <class V>
      struct types_to_tuple<V, 2>
      {
          typedef boost::tuple<typename mpl::at_n<0>::type, typename mpl::at_n<1>::type> type;
      }

      etc.

This works on mpl::vector. Getting it to work on mpl::list is
slightly more complicated.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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