return type of boost::bind

I wish to store the return value of boost::bind in an intermediate object to invoke it at a later time. I only ever see examples which combine the call to boost:bind with the immediate invocation on the returned object such as: #include <boost/bind.hpp> using namespace boost; #include <iostream> using namespace std; double f(int i, double d) { cout << "int = " << i << endl; return d; } main() { int i = 99; cout << bind(f, _1, 1.234)(i) << endl; } Instead I'd like to postpone the call to the returned object. What's the type of obj? main() { int i = 99; WHAT_IS_MY_TYPE obj = bind(f, _1, 1.234); ... cout << obj(i) << endl; } Thanks, Cheenu _________________________________________________________________ What are the 5 hot job markets for 2004? Click here to find out. http://msn.careerbuilder.com/Custom/MSN/CareerAdvice/WPI_WhereWillWeFindJobsIn2004.htm?siteid=CBMSN3006&sc_extcmp=JS_wi08_dec03_hotmail1

The return type of boost::bind is fairly indeterminate. In order to call a bound function at a later time, look at boost::function. It does a nice job of holding on to a function that uses boost::bind. --- At Sun, 1 Feb 2004 08:45:43 -0500, Cheenu Srinivasan wrote:
I wish to store the return value of boost::bind in an intermediate object to invoke it at a later time. I only ever see examples which combine the call to boost:bind with the immediate invocation on the returned object such as:
#include <boost/bind.hpp> using namespace boost; #include <iostream> using namespace std;
double f(int i, double d) { cout << "int = " << i << endl; return d; }
main() { int i = 99; cout << bind(f, _1, 1.234)(i) << endl; }
Instead I'd like to postpone the call to the returned object. What's the type of obj?
main() { int i = 99; WHAT_IS_MY_TYPE obj = bind(f, _1, 1.234); ... cout << obj(i) << endl; }
...Duane

Duane Murphy wrote:
The return type of boost::bind is fairly indeterminate. In order to call a bound function at a later time, look at boost::function. It does a nice job of holding on to a function that uses boost::bind.
I looked at "Using bind with Boost.Function" in the boost::function docs. Mimicing that, for my example, I tried: boost::function<double (int)> bound_func = boost::bind(f, _1, 1.234); int i = 99; cout << bound_func(i) << endl; but it throws a bad_function_call exception when bound_func(i) is invoked. How do I use boost::function for this situation? Thanks. Cheenu I wrote:
I wish to store the return value of boost::bind in an intermediate object to invoke it at a later time. I only ever see examples which combine the call to boost:bind with the immediate invocation on the returned object such as:
#include <boost/bind.hpp> using namespace boost; #include <iostream> using namespace std;
double f(int i, double d) { cout << "int = " << i << endl; return d; }
main() { int i = 99; cout << bind(f, _1, 1.234)(i) << endl; }
Instead I'd like to postpone the call to the returned object. What's the type of obj?
main() { int i = 99; WHAT_IS_MY_TYPE obj = bind(f, _1, 1.234); ... cout << obj(i) << endl; }

Cheenu Srinivasan wrote:
Duane Murphy wrote:
The return type of boost::bind is fairly indeterminate. In order to call a bound function at a later time, look at boost::function. It does a nice job of holding on to a function that uses boost::bind.
I looked at "Using bind with Boost.Function" in the boost::function docs. Mimicing that, for my example, I tried:
boost::function<double (int)> bound_func = boost::bind(f, _1, 1.234); int i = 99; cout << bound_func(i) << endl;
but it throws a bad_function_call exception when bound_func(i) is invoked. How do I use boost::function for this situation?
Works for me; it's probably a compiler bug. Which compiler are you using? Try boost::function<double (int)> bound_func( boost::bind(f, _1, 1.234) ); instead.

Cheenu Srinivasan wrote:
Instead I'd like to postpone the call to the returned object. What's the type of obj?
main() { int i = 99; WHAT_IS_MY_TYPE obj = bind(f, _1, 1.234); ... cout << obj(i) << endl; }
You can use a separate function template: template<class F> void test(F f) { int i = 99; std::cout << f(i) << std::endl; } int main() { test( boost::bind(f, _1, 1.234) ); }
participants (3)
-
Cheenu Srinivasan
-
Duane Murphy
-
Peter Dimov