c++11 - C++ type erasure, capture multiple methods of a single class with std::function -


consider following code, in std::function used 3 times capture methods of 1 class:

struct some_expensive_to_copy_class {     void foo1(int) const { std::cout<<"foo1"<<std::endl; }     void foo2(int) const { std::cout<<"foo2"<<std::endl; }     void foo3(int) const { std::cout<<"foo3"<<std::endl; } };  struct my_class {     template<typename c>     auto getfunctions(c const& c)     {          f1 = [c](int i) { return c.foo1(i);};          f2 = [c](int i) { return c.foo2(i);};          f3 = [c](int i) { return c.foo3(i);};     }      std::function<void(int)> f1;     std::function<void(int)> f2;     std::function<void(int)> f3; }; 

this, however, perform 3 copies of class some_expensive_to_copy_class, inefficient 1 have guessed name.

is there workaround such 1 copy made?

to emphasize it, i'm interested here in approach using std::function, not void-pointers , not corresponding inheritance-based implementation.

make copy shared_ptr, , capture that.

auto spc = std::make_shared<const c>(c);  f1 = [spc](int i) { return spc->foo1(i); } f2 = [spc](int i) { return spc->foo2(i); } f3 = [spc](int i) { return spc->foo3(i); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -