Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-04-20 10:22:55


From: "Dale Peakall" <dale.peakall_at_[hidden]>

> Bill Kempf wrote:
> > As pointed out in another post, member functions can be trivially
> > changed into function objects eliminating the need for direct support
> > by any_function. In early discussions it was seen as wasted
> > duplication of effort to address this in a "callback" class when
> > standard adapter classes were also in the pipe (i.e. boost::bind).
>
> I wouldn't class myself as an expert at these things, but I at least
> thought I had a reasonable idea, and I can't see how you can "trivially"
> convert a class, member function pair into a callback (at least on my
> totally broken MSVC compiler - no void return's, no partial
specialisation).

"Trivially" is an overoptimistic stance. :-)

BTW, MSVC 7.0b1 does support void returns; another pleasant surprise.

Take a look at the attached file for a preliminary 'bind' function. It only
supports member functions with zero or one arguments, and I haven't tested
it extensively, but it may help until 'the' boost::bind arrives.

The first form of 'bind' converts a member function pointer to a function
object that takes a pointer, a reference, or a boost::shared_ptr as its
first argument.

The second form of 'bind' converts a member function pointer, object pointer
pair into a function object.

You must define the macros BOOST_NO_VOID_RETURNS and
BOOST_NO_PARTIAL_ORDERING depending on the compiler. When both are defined
(MSVC 6.0) you have to supply a dummy integer as a last parameter to bind;
use 1, or 5, or 42, or whatever you like, but not zero (it's also a
pointer.)

Here's a short example:

#include <iostream>

// #define BOOST_NO_VOID_RETURNS
#define BOOST_NO_PARTIAL_ORDERING

#include "bind.hpp"

struct X
{
 void f() const
 {
  std::cout << "X(" << this << ")::f() const;\n";
 }

 void k()
 {
  std::cout << "X(" << this << ")::k();\n";
 }

 void g(int i)
 {
  std::cout << "X(" << this << ")::g(" << i << ");\n";
 }

 void h(int const & i)
 {
  std::cout << "X(" << this << ")::h(" << i << ");\n";
 }

 void q(int & i)
 {
  std::cout << "X(" << this << ")::q(" << i++ << ");\n";
 }
};

int main()
{
 X x;

 bind(&X::f)(x);
 bind(&X::k)(x);

 bind(&X::f, &x)();
 bind(&X::k, &x)();

 int i(5);

 bind(&X::g)(x, i);
 bind(&X::h)(x, i);
 bind(&X::q)(x, i);
 bind(&X::q)(x, i);

 bind(&X::g, &x)(i);
 bind(&X::h, &x)(i);
 bind(&X::q, &x)(i);
 bind(&X::q, &x)(i);

 return 0;
}

--
Peter Dimov
Multi Media Ltd.



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