Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2002-11-15 15:17:13


Suppose I have the following:

class Base {
  void foo(int);
};

class Derived: private Base {
public:
  void bar();
};

void Derived::bar() {
  vector<int> v;
  for_each(v.begin(), v.end(),
           boost::bind(&Base::foo,this,_1));
}

This is a reasonably common thing to want to do, except for the private
inheritance part... It bombs because in the instantiation of bind, it
knows this is a Derived *, and doesn't try to forcibly instantiate the
version which takes a Base * (even though it knows that Base::foo is a
member of Base). Then in operator() it is using a pointer to Derived to
attempt to call a function on Base. That's no good.

I think it might be possible to fix this, but it's tricky. Just
defining the partial specialization for Base * in boost::bind won't
work because it'll find the exact template match instead. I think
instead you have to define a partial specialization for all pointers in
which you use the version that takes the Base *. Unfortunately that
leads to an asymmetry for references, where I don't think you can get
away with doing the simple partial specialization without yet more
metacode. Probably doable but painful.

Anyway, it's not that hard to work around in the client code of course
(a simple static_cast<Base *>(this)), but I thought I would mention it
as a test case if someone wants to improve the user-friendliness of
boost::bind even further.

George Heintzelman
georgeh_at_[hidden]


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