gpt4 book ai didi

C++ lambda 友元

转载 作者:IT老高 更新时间:2023-10-28 12:51:15 24 4
gpt4 key购买 nike

当一个 lambda 函数在 C 类的友元函数 F 中声明时,该 lambda 函数是否可以访问 C私有(private)成员(member)?具体来说,标准允许吗?

最佳答案

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

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. ...

由于闭包类型是在友元函数中声明的,因此根据 §[class.local] 9.8/1 将具有相同的访问权限:

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 友元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23834845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com