mercredi 3 février 2016

C++ Member Function Pointer Definition

Consider this code:

struct B {
    template <class C, class M, class T>
    void call1(C (M::*member)(), T *instance) {
        std::function<void()> fp = bind(member, instance);
        fp();
    }

    template <class C, class M, class T>
    void call2(C (M::*member), T *instance) {
        std::function<void()> fp = bind(member, instance);
        fp();
    }

    void func2() {
        call1(&B::func, this); // works
        call2(&B::func, this); // works

        call1(&B::func2, this); // works
        call2(&B::func2, this); // Error: no matching member function for call to 'call2'
    }

    void func() {
        cout << "func\n";
    }

    void func2() const volatile {
        cout << "func2\n";
    }
};

It seems that the later version accepts functions with extra cv qualifiers.

What is the difference between declaring a function member pointer like this C (M::*member)() and like this C (M::*member)?

Aucun commentaire:

Enregistrer un commentaire