捕获此内容的lambda的访问权限 [英] Access rights of a lambda capturing this

查看:73
本文介绍了捕获此内容的lambda的访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

#include <iostream>

struct test
{
    void public_test()
    {
        [this]() { private_test(); }();
    }

private:
    void private_test()
    {
        std::cout << "test\n";
    }
};

int main()
{
    test().public_test();
}

lambda捕获它,然后调用捕获对象的私有方法。现在,此代码使用VC ++ 2012进行编译和工作(打印 test )。尽管这是非常直观且有用的行为,但我想知道是否可以保证按标准运行。因此,lambda是否可以私有访问通过 this 捕获的任何对象?

The lambda captures this and then calls a private method of the captured object. Now this code compiles and works (prints test) using VC++ 2012. Whereas this is quite intuitive and useful behaviour, I'd like to know if this is guaranteed to work by standard. Thus, does a lambda have private access to any object captured through this?

我试图在其中查找通过 5.1.2 [expr.prim.lambda] 读取标准,但是并不能真正找到明确的答案(对标准的深度了解不够深)。对我来说似乎唯一有用的段落是

I tried to look this up in the standard reading through 5.1.2 [expr.prim.lambda] but could not really find a definite answer (being not that well-versed in the depths of the standard). The only paragraph that seemed useful to me is


lambda表达式的类型(也是
的类型)闭包对象)是一个唯一的,未命名的不工会类类型,称为
闭包类型,其属性如下所述。此类
不是集合(8.5.1)。在
最小块范围,类范围或包含
相应lambda-expression的命名空间范围中声明闭包类型。 [注意:这确定与闭包类型(3.4.2)关联的名称空间和类的集合

lambda声明符的参数类型不会影响这些与
关联的名称空间和类。 —尾注]

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion 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. [ Note: This determines the set of namespaces and classes associated with the closure type (3.4.2). The parameter types of a lambda-declarator do not affect these associated namespaces and classes. —end note ]

但另一方面,成员函数中定义的普通本地类类型没有对周围环境的私有访问权类。因此,具有私有访问权限的lambda会以某种方式使lambda超出仅用于本地函数对象的语法糖,从而涉及更多的事情,因为它需要额外的编译器魔术以某种方式使其成为周围环境的朋友。

But on the other a normal local class type defined in a member function doesn't have private access to the surrounding class. So a lambda having private access would somehow raise lambdas above mere syntactic sugar for a local function object to something more involved, since it would need additional "compiler magic" to somehow make it a friend of the surrounding class.

那么lambda是否可以私有访问通过 this 捕获的任何对象,如果可以,标准的哪一部分允许发生这种情况?

So does a lambda have private access to any object captured through this and if yes, which parts of the standard allow this happen?

推荐答案

我认为它不是lambda的事实并不重要。 lambda所做的所有
都定义了一个本地类。并且根据§11/ 2:
成员函数的本地类可以访问与成员函数
本身可以访问的名称相同的名称。

I don't think the fact that it is a lambda is relevant. All lambda does is define a local class. And according to §11/2: "A local class of a member function may access the same names that the member function itself may access."

这篇关于捕获此内容的lambda的访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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