https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58040

--- Comment #8 from Matt Whitlock <gcc at mattwhitlock dot name> ---
Maybe this example will demonstrate it more clearly.

/* begin example */

class PublicBase {
public:
        void pub_base_pub_memb();
protected:
        void pub_base_prot_memb();
};

class ProtectedBase {
public:
        void prot_base_pub_memb();
protected:
        void prot_base_prot_memb();
};

class Derived : public PublicBase, protected ProtectedBase {
public:
        using PublicBase::pub_base_pub_memb;
        using PublicBase::pub_base_prot_memb;
        using ProtectedBase::prot_base_pub_memb;
        using ProtectedBase::prot_base_prot_memb;
};

void (Derived::*pub_base_pub_memb)() = &Derived::pub_base_pub_memb;
void (Derived::*pub_base_prot_memb)() = &Derived::pub_base_prot_memb;
void (Derived::*prot_base_pub_memb)() = &Derived::prot_base_pub_memb; // ERROR
void (Derived::*prot_base_prot_memb)() = &Derived::prot_base_prot_memb; //
ERROR

void func(Derived *d) {
        d->Derived::pub_base_pub_memb();
        d->Derived::pub_base_prot_memb();
        d->Derived::prot_base_pub_memb(); // OK
        d->Derived::prot_base_prot_memb(); // OK

        auto pub_base_pub_memb = &Derived::pub_base_pub_memb;
        auto pub_base_prot_memb = &Derived::pub_base_prot_memb;
        auto prot_base_pub_memb = &Derived::prot_base_pub_memb; // OK
        auto prot_base_prot_memb = &Derived::prot_base_prot_memb; // OK

        (d->*pub_base_pub_memb)();
        (d->*pub_base_prot_memb)();
        (d->*prot_base_pub_memb)(); // ERROR
        (d->*prot_base_prot_memb)(); // ERROR
}

/* end example */

As you can see, it is possible to call all four member functions directly using
their qualified names, but it is not possible to assign the addresses of the
members from the protected base into variables whose type is pointer to member
function of the derived class, nor is it possible to call the members of the
protected base indirectly through pointers to member function of the derived
class.

Maybe this is a defect of the language? It's certainly an asymmetry and a
surprise.

Reply via email to