Boost logo

Boost :

Subject: Re: [boost] [fusion] flatten/flatten_view Proposal
From: Eric Niebler (eniebler_at_[hidden])
Date: 2013-10-25 18:40:36


>>>> On 10/23/2013 8:35 PM, TONGARI J wrote:
>>>>>
>>>>> I just tried to re-implement it by segments_impl, but seems to cause
>>>>> memory
>>>>> violation.
>>>>>
>>>>> Code is attached. Could you shed some light on this?
>>>>> (note: flatten_view should be writable if the underlying sequence is
>>>>> writable)

OK, I see what the problem is. It's a very general problem. When you
have a "sequence" that is really generating other sequences on the fly,
and you try to iterate over that, the temporary sequences go out of
scope before the iteration succeeds. That is, begin() of a flatten_view
internally stores a cons list of iterators to (temporary) sequences. By
the time begin() returns, those sequences are gone, and the iterators
are invalid. Hence the crash.

I see in your code, you're also building a cons list of iterators, but
those are iterators to real sequences, not temporaries. So your code
doesn't exhibit the problem, at least not for your example. If you
changed your example such that the sequence was a transform_view that
generated other sequences on the fly, I'm pretty sure you'd hit the same
problem.

So, what's the answer? I haven't spent a lot of time thinking about it.
My gut reaction is: don't do that. But flattening a tree by generating
flattened views on the fly would be *really* useful. In fact, Proto
needs this. But I don't know.

-- 
Eric Niebler
Boost.org
http://www.boost.org

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk