C ++ lambda的友谊 [英] C++ lambda friendship

查看:155
本文介绍了C ++ lambda的友谊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当lambda函数声明在 C 类中的朋友的 F 函数可以访问 C 私有成员?具体来说,标准是否允许它?

When a lambda function is declared inside a function F which is a friend of class C, does the lambda function have access to C private members? Specifically, does the standard allow it?

推荐答案

C ++ 11§[expr.prim.lambda] 5.1.2 / 3 :

C++11 §[expr.prim.lambda] 5.1.2/3:


lambda表达式(也是闭包对象的类型)的类型是唯一的,未命名的非联合类类型 - 称为闭包类型 - 其属性如下所述。此类类型不是聚合(8.5.1)。闭包类型在包含相应的λ-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. ...

因为闭包类型是在friend函数内声明的,所以每个§[class.local] 9.8 / 1:

Since the closure type is declared within the friend function, it will have the same access per §[class.local] 9.8/1:


类可以在函数定义中声明;这样的类称为 local 类。局部类的名称对其包围范围是局部的。本地类在封闭范围的范围内,并且具有与包围函数相同的对函数外部的名称的访问。 ...

A class can be declared within a function definition; such a class is called a local class. The name of a local class is local to its enclosing scope. The local class is in the scope of the enclosing scope, and has the same access to names outside the function as does the enclosing function. ...

这篇关于C ++ lambda的友谊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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