Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-12-13 08:43:43


On Friday 13 December 2002 07:21 am, Greg Dehaas wrote:
> So far, I've got something like this:
>
> -------- CODE -----------------------------------------------
> #include <boost/function.hpp>
> #include <functional>
>
> //Simple Class
> class CSimple
> {
> public:
> void SimpleMethod()
> {
> printf("Simple Method Called\n");
> }
> };
>
> //Entry point
> void main()
> {
> boost::function<void> oSimpleMethod;
>
> oSimpleMethod =
> std::bind1st(std::mem_fun(&CSimple::SimpleMethod),&oSimple);
>
> oSimpleMethod();
> }

> Now it seems like it's to do with the way I'm trying to use std:bind1st
> (which I've never used before)
> However, the boost documentation shows examples doing exactly what I'm
> doing!?!
>
> Can anyone spot the mistake?
>
> Greg
>
> PS: I'm using VC++ 6

bind1st only works for binary function objects, but SimpleMethod is only
unary. The example in the Boost.Function documentation actually has a method
taking an "int" parameter, so it works. It's quite silly, actually, that
bind1st is so limited.

Anyway, with MSVC you'll want to use Boost.Bind along with Boost.Function. The
code will look like this:

  boost::function0<void> oSimpleMethod;
  oSimpleMethod = boost::bind(&CSimple::SimpleMethod, &oSimple);
  oSimpleMethod();

Other than the bind change (it's much cleaner using Boost.Bind!), I've changed
"boost::function" to "boost::function0" because MSVC can't currently handle
the boost::function class template very well. ('0' is the number of function
parameters it takes).

        Doug


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