Why is it not possible to use private method in a lambda?

后端 未结 5 1362
醉酒成梦
醉酒成梦 2020-12-10 11:23

Having a class like this:

class A {
public:
    bool hasGrandChild() const;

private:
    bool hasChild() const;
    vector children_;
};

5条回答
  •  不思量自难忘°
    2020-12-10 11:57

    Workaround:

    typedef  bool (A::*MemFn)(void) const;
    
    bool A::hasGrandChild() const {
        MemFn f = &A::hasChild;
        return any_of(childs_.begin(), childs_.end(), [=](A const &a) {
                return (a.*f)();
        });
    }
    

提交回复
热议问题