Boost logo

Boost :

Subject: Re: [boost] [range-ex] generators
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2009-02-22 10:14:29


Neil Groves skrev:
> Hi Neal,
>
> On Sun, Feb 22, 2009 at 2:05 PM, Neal Becker <ndbecker2_at_[hidden]> wrote:
>
>> I have a question of generate. I believe one usually wants generators to
>> have
>> state. Yet, generator is passed by value. This is awkward, the workaround
>> is
>> to manually specify
>
>
> I have rather blindly followed the standard and passed the Generator by
> value. I am open to deviate if there is sufficient justification.

This is difficult, as the underlying std::generate() will pass the
object by value anyway.

>> generate<ForwardRange, Generator&>( rng, gen );
>>
>> Since Generator is the 2nd template parameter we must specify ForwardRange,
>> which might be complicated.
>>
>> I wonder if we should have:
>> template<class ForwardRange, class Generator>
>> ForwardRange& generate( ForwardRange& rng, Generator & gen );
>>
>
> My usual technique which I apply to for_each and most of the other standard
> algorithm functions is to use boost::ref. The standard library passes almost
> all functors and predicates by value. I can only remember some of the
> random_... functions passing the random generator by reference.
>
> Therefore we can avoid specifying the ForwardRange thus:
>
> generate(rng, ref(gen));
>
> I am concerned that if it is wrong for generate, then it may well be wrong
> for many of the other algorithms that take functors and predicates. I
> believe that defaulting to pass by value improves the efficiency for many
> small functors.
>
> Would someone please enlighten me with the rationale for passing by value in
> the standard version?
>
> Does the use of boost::ref sufficiently address your concerns?

It would if boost::reference_wrapper<T> provided a
perfectly forwarding operator(). Maybe it's time to add that?

-Thorsten


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