2

As far as I know, the member function pointer only can be assigned to the pointer to member function type, and converted to any other except this will violate the standard, right?

And when calling std::bind(&T::memberFunc, this), it should return a dependent type which depend on T.(in std of VC++ version, it's a class template called _Binder).

So the question becomes to why one std::funcion can cover all _Binder(VC++ version) types.

class A
{
public:
    void func(){}
};
class B
{
public:
    void func(){}
};

std::function<void(void)> f[2];

A a;
B b;
f[0] = std::bind(&A::func, &a);
f[1] = std::bind(&B::func, &b);

And I can't picture what type of the member of std::funcion which stored the function would be like, unless I am wrong from the first beginning.

This question only covered the member function need to be called with it's instance.

But mine is about why one std::function type can hold all T types.

Francis
  • 737
  • 1
  • 7
  • 21

2 Answers2

3

In short what is happening is that std::bind(&A::func, &a) returns an object of a class similar to

class InternalClass
{
    A* a_;  // Will be initialized to &a
public:
    void operator()(void)
    {
        a_->func();
    }
};

[Note that this is highly simplified]

The callable operator() function is what is matches the void(void) signature of the std::function<void(void)>.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

I think the implementation would probably like this:

template</*...*/>
class std::bind</*...*/>
{
public:
    std::bind(callable_t call, param_t p)
    {
    _func = [call, p]()/* using lambda to capture all data for future calling */
    {
    p->call();
    };

}
operator std::function<void(void)>()
{

    return _func;

}
private:
std::function<void(void)> _func;
};

And lambda is the key.

Francis
  • 737
  • 1
  • 7
  • 21