|
Boost Users : |
Subject: Re: [Boost-users] [Lambda] Getting the bind incantation correct.
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2011-05-23 10:49:59
On Mon, May 23, 2011 at 2:06 PM, Robert Jones <robertgbjones_at_[hidden]> wrote:
> On Mon, May 23, 2011 at 12:43 PM, Robert Jones <robertgbjones_at_[hidden]>
> wrote:
>>
>> On Mon, May 23, 2011 at 11:42 AM, èé <xfxyjwf_at_[hidden]> wrote:
>>>
>>>
>>> On Mon, May 23, 2011 at 6:25 PM, Robert Jones <robertgbjones_at_[hidden]>
>>> wrote:
>>>>
>>>> Hi All
>>>> I'm trying to use std::vector::push_back() within a Boost.Lambda
>>>> expression, but am finding it impossible
>>>> to bind correctly. Can anyone tell me what I'm doing wrong in this code?
>>>> Thanks in advance,
>>>> - Rob.
>>>> #include <vector>
>>>> #include <boost/function.hpp>
>>>> #include <boost/lambda/bind.hpp>
>>>> void f( )
>>>> {
>>>> Â Â Â namespace ll = boost::lambda;
>>>> Â Â Â using boost::function;
>>>> Â Â Â typedef unsigned char Id;
>>>> Â Â Â typedef std::vector<Id> Ids;
>>>> Â Â Â ll::placeholder1_type x;
>>>> Â Â Â Ids ids;
>>>> Â Â Â void ( Ids::* push_back )( const Ids::value_type & ) = &
>>>> Ids::push_back;
>>>> Â Â Â function<void(Id)> my_push = ll::bind( push_back, ids, x ); // Line
>>>> 18
>>>
>>> Change ids to ll:var(ids) because the operation 'push_back' needs a
>>> mutable object.
>>>>
>>>> Â Â Â (void)my_push;
>>>> }
>>>> > g++ ignore.cpp
>>>> [...lots of template instantiation errors, then...]
>>>> ignore.cpp:18: Â instantiated from here
>>>> /usr/include/boost/lambda/detail/actions.hpp:96: error: no matching
>>>> function for call to âboost::lambda::function_adaptor<void
>>>> (std::vector<unsigned char, std::allocator<unsigned char> >::*)(const
>>>> unsigned char&)>::apply(void (std::vector<unsigned char,
>>>> std::allocator<unsigned char> >::* const&)(const unsigned char&), const
>>>> std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned
>>>> char&)â
>>>> /usr/include/boost/lambda/detail/actions.hpp:96: error: return-statement
>>>> with a value, in function returning 'void'
>>>>
>>
>> Ok, many thanks, that worked. But why isn't  "ids" mutable on its own?
>>
>
> Ahh, ok, answered my own question....
> When "ids" is passed to the binder by reference a const copy is made inside
> the binder, and it is this
> which push_back attempts to modify, producing an error. Pass by pointer or
> use var.
> Thanks again.
> - Rob.
<shameless plug>
You could have just used Phoenix:
http://beta.boost.org/doc/libs/1_47_0_snapshot/libs/phoenix/doc/html/phoenix/modules/stl/algorithm.html
It comes with push_back and all kind of other functions to support
stdlib-like containers and algorithm directly.
</shameless plug>
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