Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ lambda friendship

Tags:

c++

c++11

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?

like image 883
Anycorn Avatar asked May 23 '14 17:05

Anycorn


1 Answers

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

Since the closure type is declared within the friend function, it will have the same access per §[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. ...

like image 81
Casey Avatar answered Sep 19 '22 23:09

Casey