Boost logo

Boost :

From: Rodolfo Lima (rodolfo_at_[hidden])
Date: 2003-09-15 13:09:02


> > 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. 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))));

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. Since I think all this insert iterator adaptor is
a common problem, it deserves some special attention. My proposal naturally
extends the concept of the function std::inserter to accept a functor that
"mutates" the items as they're being inserted.

Best regards,
rod


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