Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the lambda a friend of a class?

Let's say, I have a class:

class A {
  int a;
};

And I have a lambda:

auto function = [](A* a) {
  a->a;  // <== gives an error in this line.
};

function(new A);

Is there any way to use a private member/method inside a lambda? - It's not necessary to pass the pointer to the lambda - it may be a capture-by or something else.

All reasonable schemes are welcome.

like image 719
abyss.7 Avatar asked Feb 08 '14 14:02

abyss.7


People also ask

How do you create a function as a friend of the class?

A function or class can't declare itself as a friend of any class. In a class definition, use the friend keyword and the name of a non-member function or other class to grant it access to the private and protected members of your class. In a template definition, a type parameter can be declared as a friend .

Can function of one class be a friend of another class?

Functions declared with the friend specifier in a class member list are called friend functions of that class. Classes declared with the friend specifier in the member list of another class are called friend classes of that class.

Can a function be friend of two classes?

A friend function can be friendly to 2 or more classes. The friend function does not belong to any class, so it can be used to access private data of two or more classes as in the following example. The friend functions can serve, for example, to conduct operations between two different classes.

How do you declare a friend function outside class?

Syntax of friend functions: To make a function that is declared outside the class “friendly” to that class, we have to declare the function as a friend function, as seen below: class className{ // Other Declarations friend returnType functionName(arg list); };


2 Answers

You can do it by creating a friend function that returns the lambda function. It inherits the friend access:

struct A {
  friend std::function<void(A&, int)> f();

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

std::function<void(A&, int)> f() {
  return [] (A &a, int i) {a.i = i; a.test(); };
}

int main() {
    A a;
    f()(a, 13);

    return 0;
}
like image 168
Jaa-c Avatar answered Oct 21 '22 01:10

Jaa-c


using std::function takes extra resource, so I recomendet using friend/or method function to access private member (friend function implicit inlined):

class A{
    int a;

    friend int access_member(A*a){ return a->a;}
};

-----------------------------------------
auto function = [](A*a){   return access_member(a); }

Live example

EDIT: I personally like std::function, but don't forgot, std::function always takes extra memory resources, and may not inlined , so if you may implement your source without std::function, don't use std::function. See, How is std::function implemented? Also, Lambda to std::function conversion performance

like image 30
Khurshid Avatar answered Oct 21 '22 00:10

Khurshid