Dlaczego nie można użyć metody prywatnej w lambdzie?
Posiadanie klasy takiej jak ta:
class A {
public:
bool hasGrandChild() const;
private:
bool hasChild() const;
vector<A> children_;
};
Dlaczego nie można użyć metody prywatnejhasChild()
w wyrażeniu lambda zdefiniowanym w metodziehasGrandChild()
lubię to?
bool A::hasGrandChild() const {
return any_of(children_.begin(), children_.end(), [](A const &a) {
return a.hasChild();
});
}
Kompilator wydaje błąd, że metodahasChild()
jest prywatny w kontekście. Czy jest jakieś obejście?
Edytować: Wygląda na to, że kod, który napisałem, pierwotnie działa. Myślałem, że to jest równoważne, ale kod, którynie działa w GCC jest bardziej podobny do tego:
#include <vector>
#include <algorithm>
class Foo;
class BaseA {
protected:
bool hasChild() const { return !children_.empty(); }
std::vector<Foo> children_;
};
class BaseB {
protected:
bool hasChild() const { return false; }
};
class Foo : public BaseA, public BaseB {
public:
bool hasGrandChild() const {
return std::any_of(children_.begin(), children_.end(), [](Foo const &foo) {
return foo.BaseA::hasChild();
});
}
};
int main()
{
Foo foo;
foo.hasGrandChild();
return 0;
}
Wydaje się, że istnieje problem z pełnymi nazwami jakto nie działa, aleto działa.