I'd like to std::bind
to a member function from a private base class, made "public" with a using
-declaration in the derived class. Calling the function directly works, but it seems binding or using member function pointers doesn't compile:
#include <functional>
struct Base {
void foo() { }
};
struct Derived : private Base {
using Base::foo;
};
int main(int, char **)
{
Derived d;
// call member function directly:
// compiles fine
d.foo();
// call function object bound to member function:
// no matching function for call to object of type '__bind<void (Base::*)(), Derived &>'
std::bind(&Derived::foo, d)();
// call via pointer to member function:
// cannot cast 'Derived' to its private base class 'Base'
(d.*(&Derived::foo))();
return 0;
}
Looking at the error messages above, the issue seems to be that Derived::foo
is still just Base::foo
, and I can't access Base
through Derived
outside Derived
itself.
This seems inconsistent - should I not be able to use direct calls, bound functions, and function pointers interchangeably?
Is there a workaround that would let me bind to foo
on a Derived
object, preferably without changing Base
or Derived
(which are in a library I don't own)?