Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-11-28 07:02:01


Matt S Trentini wrote:
> Peter Dimov wrote:
>
>> When you construct a boost::function from &HandleClass::handle, what
>> actually gets stored is boost::mem_fn(&HandleClass::handle). See
>>
>> http://www.boost.org/libs/mem_fn/
>>
>> Even
>>
>> std::vector< boost::shared_ptr<HandleClass> > listOfHcs;
>>
>> will work.
>
> Thanks Peter, makes sense after reading that documentation now.
>
> I've actually got another question - I'd really prefer to replace
> ReturnContainer with an output iterator type. So Extract would look
> like this:
>
> template
> <
> class InIterator,
> class OutIterator,
> >
> void Extract(const InIterator & itBegin,
> const InIterator & itEnd,
> OutIterator & itOut,
> boost::function1<OutIterator::value_type,
> FwdIterator::value_type> f)
> {
> for (FwdIterator it = itBegin ;it != itEnd; ++it)
> {
> *itOut++ = f(*it);
> }
> };
>
> And then you'd typically call Extract with an inserter:
>
> Extract(listOfHcs.begin(), listOfHcs.end(),
> back_inserter(needToFill), &HandleClass::handle);
>
> 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.

[...]

> Am I overlooking something here?

1. Iterators are typically passed by value.
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. :-)

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


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