Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I assign a member function pointer to an overloaded function without using a type alias?

See below example. Can’t think of a reason why this is not possible. Am I really forced to introduce a using decl or typedef? Any other reasonable workarounds?

struct Foo {
    public:
        int get_res() { return 1; }
        int get_res(int i) { return 1; }
};


// Works!
struct Accessor {
    using overload = int(Foo::*)();
    static constexpr overload resGetter = &Foo::get_res;
};

// error: expected unqualified-id before ')' token
struct Accessor {
    static constexpr int(Foo::*)() resGetter = &Foo::get_res;
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

struct Accessor2 {
    static constexpr int(Foo::*resGetter)() = &Foo::get_res;
};

That’s why you don’t usually write member function pointers like this — it looks strange, and people make errors.

Typedefs do not generate any code — you can use them as much as you want to make your code readable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading