Boost logo

Boost :

From: Samuel Krempp (krempp_at_[hidden])
Date: 2003-09-16 14:05:57


le Monday 15 September 2003 20:09, rodolfo_at_[hidden] écrivit :

>> > That's it. I don't know if there's any other way to do it using
>> > existing libraries. If there is, please let me know.
>>
>> Have you looked at boost/function_output_iterator.hpp?
>
> Yes, I had. The problem is that using make_function_output_iterator you
> can't use lambda to make a functor to insert something in a container.
> Because in a container there's a template-based overload of insert (the
> one that accepts iterators), you can't call the proper insert (the one
> that accepts the data to be inserted itself). The compiler (VC7.1) says
> that I can't use a function template (the insert that accepts iterators)
> as a function argument. At least I don't know if it's possible to select
> the proper overload.

check the lambda documentation, you can use an overloaded member function
but you have to either select the one you want (static_cast or assign it
to a variable).

> Using my example and make_function_output_iterator,
> this try fails:
>
>
add_random_integer(make_function_output_iterator(lambda::bind(&set<data>::in
> sert,&setData,lambda::bind<data>(lambda::constructor<data>(), lambda::_1,
> 10))));

but this works :
    std::pair<set<data>::const_iterator, bool> (
        set<data>::* insert_pf) (const data&) = & set<data>::insert;
    add_random_integer(
        boost::make_function_output_iterator(
            bind(insert_pf, &setData,
                 bind<data>(constructor<data>(), _1, 10))));

> I should do something like the following
>
> class my_inserter_functor
> {
> public:
> my_inserter_functor(CONT _cont, FUNC _fn) : cont(_cont), fn(_fn) {};
> void operator()(const int &a)
> {
> cont.insert(fn(a));
> }
> private:
> CONT &cont;
> FUNC fn;
> };
>
> template<class CONT, class FUNC>
> my_inserter_functor<CONT, FUNC> make_my_inserter(CONT &_cont, FUNC _fn)
> {
> return my_inserter_functor<CONT, FUNC>(_cont, _fn);
> }
>
>
add_random_number(make_function_output_iterator(make_my_inserter(setData,lam
> bda::bind<data>(lambda::constructor<data>(), lambda::_1, 10))));
>
> which is kind of verbose.

I think it's not that bad. If you plan to do a lot of inserting, it's better
than taking the address of the insert member function..

regards,

-- 
Samuel

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