c++ - Using std::function with union in C++11 -


i trying define class has functions different numbers of parameters stored in union. class initialized function object , necessary parameters. getting these compiler errors @ locations marked (1) , (2)

(1) destructor of '_' implicitly deleted because variant field 'f_v' has non-trivial destructor

(2) destructor of 'myclass' implicitly deleted because field 'functions' has deleted destructor

i want declare union of different function objects, , depending on parameters passed-in able pick appropriate function , call it. why getting these errors , how should rewrite code?

template<typename variable> struct myclass {     myclass(variable *p, const std::function<void (variable&)>& f) {         functions.f_v = f;         ...     } protected:     union _ {         std::function<void (variable &)> f_v; // (1)         std::function<void (const variable &, variable &)> f_vv;         std::function<void (const variable &, const variable &, variable &)> f_vvv;         std::function<void (const variable &, const variable &, const variable &, variable &)> f_vvvv;     } functions; // (2)     ...  }; 

edit: trying accomplish storage of lambda functions having variable argument lists in container. this:

std::vector<myclass<myvariable>> mycontaineroflambdas; myvariable a,b,to; auto lambda1 = [this](const myvariable& a, myvariable& to) { ... }; mycontaineroflambdas.push_back(myclass<myvariable>(a,to,lambda1)); auto lambda2 = [this](const myvariable& a, const myvariable& b, myvariable& to) { ... }; mycontaineroflambdas.push_back(myclass<myvariable>(a,b,to,lambda2)); ... // iterate on stored myclass objects holding lamda functions , parameters // call them for(myclass<myvariable> & e:mycontaineroflambdas) {   e(); // here appropriate lambda function invoked appropriate values   // stored in myclass } 

note: brevity have omitted definition of () operator in myclass, calls right function object right parameters.

if see better design approach this, fine, please direct me in right direction. thanks!

you can use unrestricted unions feature of c++11 want. need implement custom constructor , destructor on union constructs/deletes appropriate active member.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -