I have a lambda that I need to convert to a callable object so that I can specialize the call statement. My impression has always been that a signed lambda is void(auto)equivalent to a called structure like this:
struct callable {
Foo & capture;
template< typename T >
void operator()( T arg ) { }
}
However, lambda can access private and protected members when declared inside a member function.
Here is a simplified example:
#include <iostream>
using namespace std;
class A {
protected:
void a() { cout << "YES" << endl; }
};
class B : public A {
public:
void call1();
void call2();
};
struct callable {
B * mB;
void operator()() {
}
};
void B::call1() {
[&]() { a(); }();
}
void B::call2() {
callable c{ this };
c();
}
int main()
{
B b;
b.call1();
b.call2();
}
Is there a way to imitate this behavior in the called structure without declaring it in the header and making it a class of friends? This seems problematic because I will have many different challenges. I am also just curious because I got the impression that lambdas are functionally identical to declaring a structure using a call statement.
, , , , . , .