How to provide a callable object protected access like lambda in C++? -


i have lambda need convert callable object can specialize call operator. impression has been lambda void(auto) signature equivalent callable struct this:

struct callable {     foo & capture;      template< typename t >     void operator()( t arg ) { /* ... */ } } 

however, lambda can access private , protected members when declared within member function.

here's simplified example:

#include <iostream>  using namespace std;  class { protected:     void a() { cout << "yes" << endl; } };  class b : public { public:     void call1();     void call2(); };   struct callable {     b * mb;      void operator()() {         // not compile: 'void a::a()' protected within context         // mb->a();     } };  void b::call1() {     // how access a() ?!     [&]() { a(); }(); }  void b::call2() {     callable c{ };     c(); }   int main() {     b b;      b.call1();     b.call2(); } 

is there way emulate behavior in callable struct, without declaring in header , making friend class? seems problematic because i'm going have lot of different callables. i'm curious it, because under impression lambdas functionally identical declaring struct call operator.

access rights of lambda capturing this seems lambda has same access local class. in case, need emulate generic lambda, , local classes can't have template member functions.

you can still capture this , &b::a

struct callable {     b* mb;     void (a::*m)();      void operator()() const {         (mb->*m)();     } };  void b::call2() {     callable c{ this, &b::a };     c(); } 

demo


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -