Boost logo

Boost Users :

From: Andreas Klöckner (lists_at_[hidden])
Date: 2007-09-03 18:46:57


Hi all,

I came up with the following implementation of a "flatten" operation that
turns a sequence of sequences into a flat list:

8< -------------------------------------------------------------------
#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/clear.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/size.hpp>

template<class Sequence>
struct flatten
{
  typedef typename boost::mpl::fold<
    Sequence,
    typename boost::mpl::clear<Sequence>::type,
    typename boost::mpl::copy<
      boost::mpl::_2,
    boost::mpl::back_inserter<boost::mpl::_1>
>::type
>::type
      type;
};

typedef
  flatten<
    boost::mpl::vector<
      boost::mpl::vector_c<int, 0, 1, 2, 3>,
      boost::mpl::vector_c<int, 4, 5, 6>
>
>::type
    flatvec;

int main()
{
  std::cout << boost::mpl::size<flatvec>::value << std::endl;
}
8< -------------------------------------------------------------------

It compiles (using gcc 4.2.1), but prints 0 instead of the expected 7. What am
I doing wrong?

Thanks for your help
Andreas




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