vendredi 5 janvier 2018

Why can't I pass a member function as a paramater (but can pass lambda of same function)

I want to pass a member function into another.

I can pass in non-member func, func_non_member.

I can pass in member func wrapped in lambda, func_wrapped.

But, I can't pass in member func, func, why? Is it OK to use the wrapped version?

#include <iostream>
#include <functional>

void func_non_member() { 
    std::cout << "Hi";
}

class M {
    public:
    void func() {
        std::cout << "Hi";
    };
    void do_a_func(std::function<void(void)> f) {
        f();
    }
    void go() {
        auto func_wrapped = [&](){func();};
        do_a_func(func_wrapped);
        do_a_func(func_non_member);
        // do_a_func(func);  // Can't do this
    }
};


int main(){
    M m;
    m.go();
}

Aucun commentaire:

Enregistrer un commentaire