Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind] How to dereference _1 in a boost::bindexpression
From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2008-10-02 11:16:55


Peter Barker wrote:
> On Thu, Oct 2, 2008 at 3:25 PM, Peter Dimov <pdimov_at_[hidden]> wrote:
>> Peter Barker:
>> ...
>>> void accept(TheClass& tc)
>>> {
>>> }
>>>
>>> int main()
>>> {
>>> std::vector<TheClass*> cont;
>>>
>>> // Compile error - how to dereference _1 ???
>>> std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1));
>>> }
>>>
>>> How can I dereference _1 so I can use the accept function?
>> With boost::bind, there's no easy way to dereference _1. You'd need
>>
>> template<class T> T& deref( T* p ) { return *p; }
>>
>> std::for_each( cont.begin(), cont.end(),
>> boost::bind( accept, boost::bind( deref<TheClass>, _1 ) )
>> );
> Peter,
>
> It's nice to hear the answer from the horses mouth (so to speak)!

IIRC, indirect_iterator takes this approach. The following should work
as well:

std::for_each( make_indirect_iterator(cont.begin())
              , make_indirect_iterator(cont.end())
              , boost::bind( accept, _1));

Jeff Flinn


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