Boost logo

Boost Users :

From: Matt S Trentini (matt_s_trentini_at_[hidden])
Date: 2003-11-28 21:33:30


Heya Peter,

Peter Dimov wrote:

>>But this doesn't compile because the inserters (at least on my
>>platform) don't define value_type.
>
> They aren't required to. You should use
> std::iterator_traits<It>::value_type.

Ah, makes sense.

>>Am I overlooking something here?
>
> 1. Iterators are typically passed by value.

I've noticed that - is there are reason for this?

My reason for passing by const reference is mostly academic anyway
because copying iterators is typically cheap, I just have a curious
nature... :)

> 2. You don't need to use boost::function in a template. It is typically only
> used when you want a fixed compile-time signature.
>
> template<class InIt, class OutIt, class F>
> void Extract(InIt first, InIt last, OutIt out, F f)
> {
> for (; first != last; ++first, ++out)
> {
> *out = f(*first);
> }
> }
>
> Now look at std::transform. :-)

Oh the shame of it! ;) Covered with embarrassment I've dutifully
erased my Extract function... :)

Kinda funny how sometimes you can't see the wood through the
trees...only yesterday I recommended that a colleague use transform for
(what I now recognise as) a similar application. I got hung up on the
fact that I wanted to use boost::function I guess...

> (You'll no longer be able to pass &X::f directly, though, use mem_fn
> explicitly.)

Got it.

For those that are curious here's the improved solution that Peter was
alluding to:

     std::transform(listOfHcs.begin(),
                    listOfHcs.end(),
                    back_inserter(needToFill),
                    boost::mem_fn(&HandleClass::handle));

Thanks Peter - both for your help and your work in boost! :)

Cheers,
Matt


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