为什么不能在lambda中使用私有方法? [英] Why is it not possible to use private method in a lambda?

查看:251
本文介绍了为什么不能在lambda中使用私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样的类:

class A {
public:
    bool hasGrandChild() const;

private:
    bool hasChild() const;
    vector<A> children_;
};

为什么不能使用私有方法 hasChild code>在方法 hasGrandChild()中定义的lambda表达式是这样吗?

Why is it not possible to use a private method hasChild() in a lambda expression defined in the method hasGrandChild() like this?

bool A::hasGrandChild() const {
    return any_of(children_.begin(), children_.end(), [](A const &a) {
        return a.hasChild();
    });
}


$ b <
在上下文中是私有的。是否有任何解决方法?

Compiler issues an error that the method hasChild() is private within the context. Is there any workaround?

编辑:
看来,我发布的代码原来工作。我认为它是等效的,但不能在GCC 上工作的代码更像这样:

It seems that the code as I posted it originally works. I thought that it is equivalent, but the code that does not work on GCC is more like this:

#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;
}

似乎全限定名称有问题这不工作,但这个工作

Seems that there is a problem with fully qualified names as this does not work, but this works.

推荐答案

当lambda尝试访问受保护的成员时,它似乎只是一个特殊情况下的GCC错误从父类使用完全限定名。 这不起作用

It seems to be just a GCC bug in a special case when the lambda tries to access a protected member from parent class using fully qualified name. This does not work:

class Base {
protected:
    bool hasChild() const { return !childs_.empty(); }
    std::vector<Foo> childs_;
};

class Foo : public Base {
public:
  bool hasGrandChild() const {
    return std::any_of(childs_.begin(), childs_.end(), [](Foo const &foo) {
      return foo.Base::hasChild();
    });
  }  
};

,但 this works

class Foo : public Base {
public:
  bool hasGrandChild() const {
    return std::any_of(childs_.begin(), childs_.end(), [](Foo const &foo) {
      return foo.hasChild();
    });
  }  
};

根据C ++ 11,5.1.2 / 3:

According to C++11, 5.1.2/3:


lambda表达式的类型(也是
闭包对象的类型)是一个唯一的未命名非联合类类型 - 称为
闭包类型 - 其属性如下所述。此类类型
不是聚合(8.5.1)。
最小的块范围,类范围或包含
对应的lambda-expression
的命名空间范围中声明闭包类型。

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression.

然后C ++ 11,11.7 / 1:

And then C++11, 11.7/1:


A nested class is a member and as such has the same access rights as any other member.

因此,所提到的函数局部lambda应该是一个成员,因此具有与
任何其他成员相同的访问权限。具有与该类的任何其他成员相同的访问权限。因此,它应该能够从父类调用受保护的方法。

So the mentioned function-local lambda should have the same access rights as any other member of the class. Therefore it should be able to call a protected method from a parent class.

这篇关于为什么不能在lambda中使用私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆