Convert member function pointer to function pointer [duplicate]

I’d like to have something like this:

struct Foo {
    template <typename T>
    void operator()(T i) const { /* ... */
    }

    template <typename T>
    auto To() const {    // Doesn't work!
        return this->*static_cast<void (Foo::*)(int) const>(&Foo::operator());
    }
};

Basically, I’d like to obtain a functor from a member function pointer. The above code would have worked if instead of operator() I had used a static function (having gotten rid of all the this->* and Foo:: parts). If I only return the member function pointer auto ptr = static_cast<void (Foo::*)(int) const>(&Foo::operator()), I can use it, e.g., by providing a Foo* pointer foo as foo->*ptr(0);: this motivates this->* in the above code. Is there a nice way to achieve this objective without resorting to lambdas? I’d like to just return a function pointer that "embeds" the information of the specific Foo instance from which it was created.

P.S.: I don’t want solutions with lambdas because I’m only curious about solutions without them.

Source: Windows Questions C++

LEAVE A COMMENT