Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-06-27 08:30:40


Nico Massi wrote:
> On Sat, 26 Jun 2004 13:44:54 -0400, David Abrahams
> <dave_at_[hidden]> wrote:
>
>> "Nico Massi" <desertswat_at_[hidden]> writes:
>>
>>> Hi,
>>>
>>> Is it possible to store a boost::bind?
>>
>> No; boost::bind is a function template.
>>
>>> boost::bind<int> x( f, _1, _2 );
>>> int i = x( 3, 4 );
>>>
>>> I get errors if i want to declare a variable of type boost::bind.
>>> The point is:
>>>
>>> I want to have an object that gets a boost::bind, stores it and
>>> executes it with
>>> the correct values to the time i want.
>>> How can i do this?
>>
>> Maybe you want
>>
>> boost::function<int(int,int)> x = boost::bind(f,_1,_2);
>>
>> ??
>>
>
> Better than nothing,
> but i need a variable number of arguments, if it is possible.

Perhaps if you describe your problem in more detail we'll be able to suggest
a better solution.

In general function objects are stored in three ways:

1. In a boost::function<>, as above; but you need a fixed signature.

2. In a function template argument:

template<class It, class F> void for_each( It first, It last, F f )
{
    // f is 'stored' here
}

for_each( first, last, boost::bind(f, _1, _2) );

3. In a helper class template that implements a specified interface:

template<class F> class my_function_wrapper: public my_function_base
{
private:

    F f_;

public:

    explicit my_function_wrapper(F f): f_( f ) {}

    virtual void do0() { f_(); }
    virtual void do1(int x) { f_(x); }
    // and so on
};


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