Boost logo

Boost :

Subject: Re: [boost] [Bind] How do I get bind to deduce the return type of for_each?
From: Daniel Walker (daniel.j.walker_at_[hidden])
Date: 2010-04-19 13:39:53


On Sat, Apr 17, 2010 at 11:50 AM, Roland Bock <rbock_at_[hidden]> wrote:
> Daniel Walker wrote:
>>
>> On Sat, Apr 17, 2010 at 8:58 AM, Roland Bock <rbock_at_[hidden]> wrote:
>>
>>>
>>> Daniel Walker wrote:
>>>
>>>>
>>>> On Sat, Apr 17, 2010 at 7:25 AM, Roland Bock <rbock_at_[hidden]> wrote:
>>>>
>>>>>
>>>>>  for_each(vov.begin(), vov.end(),
>>>>>      bind(for_each< vocType::iterator, function<void (myClass&)> >,
>>>>>         bind<vocType::iterator>(&vocType::begin, _1),
>>>>>         bind<vocType::iterator>(&vocType::end, _1),
>>>>>         protect(bind(&myClass::func, _1))
>>>>>         )
>>>>>      );
>>>>>
>>>>>
>>>>
>>>> Oh yeah, that's a good idea! Instantiate std::for_each with a void
>>>> boost::function! That's a portable solution as well, since bind and
>>>> function are available on practically any compiler, even ancient,
>>>> substandard ones.
>>>>
>>>> Daniel Walker
>>>>
>>>>
>>>>
>>>
>>> Thanks :-)
>>>
>>> Btw, could I do the same with tr1::bind? What would be the equivalent of
>>> boost::protect?
>>>
>>
>> Yeah, you can do the same thing in TR1. Instead of boost::protect, you
>> can just use tr1::function, again. :)
>>
>>   for_each(vov.begin(), vov.end(),
>>         bind(for_each< vocType::iterator, function<void (myClass&)> >,
>>            bind<vocType::iterator>(&vocType::begin, _1),
>>            bind<vocType::iterator>(&vocType::end, _1),
>>            function<void(myClass&)>(bind(&myClass::func, _1))
>>            )
>>         );
>>
>> Actually, I once wrote a functional_cast (analogous to lexical_cast)
>> that did something like that. It's in vault.
>>
>> Daniel Walker
>>
>
> OK, got it, the function object kind of digests the inner _1 before the
> outer bind can lay its fingers on it :-)

Exactly. :) And moreover, with respect to TR1/c++0x,
is_bind_expression has not been specialized for the function object's
type, so it's not treated as a sub-bind expression.

>
> Just took a look at the polymorphic_function and functional_cast. I'll have
> to try and remember it, because I am sure I had use cases for that in the
> past (obviously solved them in another way). How would you apply them to the
> current topic?

Oh, I was just mentioning it as an aside, because the idiom looks
similar; e.g. wrapping things with function<signature>(object).
functional_cast can be used to change the signature in order to
achieve a polymorphic effect when the underling object can support
multiple call signatures. But that's not the issue here.

Daniel Walker


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