Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] iterating over a sequence with non-const references
From: Joel de Guzman (joel_at_[hidden])
Date: 2012-04-02 01:34:02


On 4/2/2012 3:56 AM, paul Fultz wrote:
>
>
>>> Hi,
>>> I would like to iterate over a sequence and preserve the
>>>
>>> reference/const/non-const of the elements. That is say I have
>>> a sequence like this:
>>>
>>> tuple<int&, int> seq;
>>>
>>> I would like to this to be true:
>>>
>>> typeof(deref(begin(seq))) == int&
>>> typeof(deref(next(begin(seq)))) == int&&
>>>
>>> It seems fusion sequences always return a const reference to
>>> the element. Is there a workaround to get it to work like this?
>>
>> Trust me, you don't want that, for roughly the same reason you don't
>> want regular sequence rvalues to have rvalue-returning iterators:
>> https://groups.google.com/d/msg/comp.lang.c++.moderated/1BwNTHCFiOs/L1T3WC9T_kMJ
>
> I can see how that would be bad. Im using it to invoke functions which
> wouldn't be a problem for that, but other use cases would cause problems.
> Right now, it won't return a non-const reference from the iterator for tuples.
>
>>
>>> Or is this just not possible with Boost.Fusion?
>>
>> I think you'd need to create your own iterator fusion-style iterator for
>> this purpose.
>>
>>> Furthermore, I would like to use the transformation algorithms
>>> (especially push_back, and push_front) and still preserve the
>>> reference type.
>>
>> Sorry, I don't know what that means. How could using an algorithm
>> change a sequence's (or iterator's?) reference type?
>
> Well for example in push_back, it adds const to the sequence like this:
>
> result_of::push_back<Sequence const, T>
>
> Which will cause all references in the sequence to become const, as
> well.
>
> Thanks, for the reply, but im thinking about just implementing an invoke
> that works just for tuples, but multiple tuples can be given. That way I won't
> need to use push_back and push_front. I will just need to find a way to
> bring together all the tuples before the invoke.

push_back is designed to be non-mutating. That is why. This is
true of all the views returned from 'algos', by design. What
you can do is to make a container from the view through, e.g.
as_vector, as_list. It is *not* true that all sequences return
const refs to its elements. Containers definitely return references,
otherwise get<N>(c)= expr will not be possible.

(That being said, I'm considering allowing mutable views)

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://boost-spirit.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