Why don't you try putting all your functions in std :: function inside std :: vector and then swap the vector?
vector has noexcept constructor for move.
EDIT:
To fully expand the necessary concepts, I can not answer in the comments.
For your situation (different function signatures), you will need to erase the styles and some dynamic clicks.
Here is some code that uses all of these concepts in the content (vector and enumeration and others).
Now you can save the vector of your lambdas, regardless of the signature.
class LambdaContainer { public: struct GenericContainer { virtual ~GenericContainer(){} }; template <typename T> struct SpecializedContainer : GenericContainer { SpecializedContainer(const T& t) : lambda(t){} virtual ~SpecializedContainer(){} T lambda; }; private: std::shared_ptr<GenericContainer> myLambda; public: template <typename T> LambdaContainer(const T& aLambda) : myLambda(new SpecializedContainer<T>(aLambda)){} std::shared_ptr<GenericContainer> getContainedLambda() { return myLambda; } }; enum eFunctions { FCT_INT=0, FCT_FLOAT=1 }; ... int main() { int aa = 10; float b = 3.0f; std::function<void(int)> lambda1 = [aa](int arg) { printf("at this time b plus argument is %d\n ",aa+arg); }; std::function<int (float, float )> lambda2 = [b] (float arg1, float arg2) { printf("calling the sum of floats %f , %f\n"); return (int)(arg1+arg2+b);}; std::vector<LambdaContainer> lambdaVector; lambdaVector.push_back(LambdaContainer(lambda1)); lambdaVector.push_back(LambdaContainer(lambda2)); std::shared_ptr<LambdaContainer::GenericContainer> container = lambdaVector[FCT_INT].getContainedLambda(); LambdaContainer::SpecializedContainer<std::function<void(int)> >* ptr = dynamic_cast<LambdaContainer::SpecializedContainer<std::function<void(int)> >*> (container.get()); if (ptr!=NULL) { std::function<void(int)> extractedLambda = ptr->lambda; extractedLambda(5); } std::shared_ptr<LambdaContainer::GenericContainer> container2 = lambdaVector[FCT_FLOAT].getContainedLambda(); LambdaContainer::SpecializedContainer<std::function<int(float,float)> >* ptr2 = dynamic_cast<LambdaContainer::SpecializedContainer<std::function<int(float,float)> >*> (container2.get()); if (ptr2!=NULL) { std::function<int(float,float)> extractedLambda = ptr2->lambda; printf("the sum is %d\n",extractedLambda(3.0f,2.0f)); }
}
source share