boost::bind with protected members & context

前端 未结 3 584
不知归路
不知归路 2020-12-17 01:29

In the below code, there are two \"equivalent\" calls to std::for_each using boost:bind expressions. The indicated line compiles, the indicated fai

3条回答
  •  爱一瞬间的悲伤
    2020-12-17 01:42

    The reason for this restriction is enforcement of access control across different classes that share a common base.

    This is reinforced by notes in Core Language Defects Report defect #385, the relevant part copied here for reference:

    [...] the reason we have this rule is that C's use of inherited protected members might be different from their use in a sibling class, say D. Thus members and friends of C can only use B::p in a manner consistent with C's usage, i.e., in C or derived-from-C objects.

    As an example of something this rule prevents:

    class B {
    protected:
        void p() { };
    };
    
    class C : public B {
    public:
        typedef void (B::*fn_t)();
        fn_t get_p() {
            return &B::p; // compilation error here, B::p is protected
        }
    };
    
    class D : public B { };
    
    int main() {
        C c;
        C::fn_t pbp = c.get_p();
        B * pb = new D();
        (pb->*pbp)();
    }
    

    The protected status of D::p is something we want the compiler to enforce, but if the above compiled that would not be the case.

提交回复
热议问题