store a boost::bind

Hi, Is it possible to store a boost::bind? 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? Ciao

"Nico Massi" <desertswat@gmx.de> 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); ?? -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

On Sat, 26 Jun 2004 13:44:54 -0400, David Abrahams <dave@boost-consulting.com> wrote:
"Nico Massi" <desertswat@gmx.de> 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.

"Nico Massi" <desertswat@gmx.de> writes:
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.
boost::function doesn't support dumping arguments, unless I'm mistaken. Do this: template <class T> struct incomplete; template <class T> int q(T) { incomplete<T> a; } int x = q(boost::bind<int>(f,_1,_2)); The error message will tell you that incomplete<some_nasty_type> is incomplete. Write down that type and use it to declare the result of calling boost::bind. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

"David Abrahams" <dave@boost-consulting.com> wrote
The error message will tell you that incomplete<some_nasty_type> is incomplete.
Write down that type and use it to declare the result of calling boost::bind.
... or use typeof if your compiler supports it ... BTW, it may be easy to define boost::bind support layer on top of my typeof emulation if there is an interest for this... Arkadiy

Nico Massi wrote:
On Sat, 26 Jun 2004 13:44:54 -0400, David Abrahams <dave@boost-consulting.com> wrote:
"Nico Massi" <desertswat@gmx.de> 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 };

I have solved it with boost:function and a wrapper, so that i have a fixed signature. Thx all
participants (4)
-
Arkadiy Vertleyb
-
David Abrahams
-
Nico Massi
-
Peter Dimov