Boost logo

Boost Users :

From: Tiago de Paula Peixoto (tiago_at_[hidden])
Date: 2007-12-25 11:21:14


On 09/03/2007 07:46 PM, Andreas Klöckner wrote:
> Hi all,
>
> I came up with the following implementation of a "flatten" operation
> that turns a sequence of sequences into a flat list:

[...]

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

I'm not sure of what you might be doing wrong, but if you take away the
lambda expression, and use another level of indirection things work for
me:

----- cut ------
#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>

// joins two sequences
struct join
{
    template <class Seq1, class Seq2>
    struct apply
    {
        typedef typename boost::mpl::copy<
            Seq2,
            boost::mpl::back_inserter<Seq1>
>::type type;
    };
};

template<class Sequence>
struct flatten
{
    typedef typename boost::mpl::fold<
        Sequence,
        typename boost::mpl::clear<Sequence>::type,
        join
>::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;
}
----- cut ------

I hope it helps... Did you manage to fix this some other way?

Cheers,
Tiago

-- 
Tiago de Paula Peixoto <tiago_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