
On Sat, 15 Mar 2003 12:22:34 -0500, Douglas Gregor <gregod@cs.rpi.edu> wrote:
On Friday 14 March 2003 09:45 pm, Faheem Mitha wrote:
Dear People,
I have defined a function as follows.
double accept_prob_fn(const double x, const double be, const double theta, const double a, const double b);
Now, I define the temporary function object
bind(accept_prob_fn, _1, be, theta, a, b);
So this function object would return the first argument, x. However, I want to save this function object to a function object called foo, say.
So I want to do something like
Type foo = bind(accept_prob_fn, _1, be, theta, a, b);
However, the problem I have is that I'm not sure what type foo should be. [snip]
The type is "unspecified", may vary from compiler to compiler, and is hideous on every compiler. For these reasons, it's intentional that you can't name the type directly (see http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1438.htm, section IIIB).
However, you can include <boost/function.hpp> and make the Type: boost::function<double(double x)>
(or, if your compiler isn't up to par, boost::function1<double, double>). See the Boost.Function documentation for more information about the Function library.
Thanks, this is very helpful. boost::function<double(double x)> works fine with gcc 3.0. Nice simple syntax; takes a double argument and returns a double. A small comment. The paper you quote above (http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1438.htm) does clearly state that the return type is unspecified, but does not say anything further (as far as I can see) about how to handle assignments like above, and it is presumably not part of the official documentation anyway. Also, the bind documentation itself (again, as far as I can see) does not say anything about how to handle assignments. Therefore, would it not be a good idea to include an example in the bind documentation showing how assignments should be handled? In any case, more and varied examples are always a good thing. Bear in mind I don't have much C++ experience, so the needed syntax may have been quite clear to a more experienced person. Thanks again for your help. Faheem.