I want to create a std :: vector object (or any other standard or custom container type) with custom and arbitrary function elements whose signatures are all the same.
It should be something like this:
// Define the functions and push them into a vector std::vector<????> MyFunctions; MyFunctions.push_back(double(int n, float f){ return (double) f / (double) n; }); MyFunctions.push_back(double(int n, float f){ return (double) sqrt((double) f) / (double) n; }); // ... MyFunctions.push_back(double(int n, float f){ return (double) (f * f) / (double) (n + 1); }); // Create an argument list std::vector<std::pair<int, float>> ArgumentList; // ... // Evaluate the functions with the given arguments // Suppose that it is guarantied that ArgumentList and MyFunctions are in the same size std::vector<double> Results; for (size_t i=0; i<MyFunctions.size(); i++) { Results.push_back(MyFunctions.at(i)(ArgumentList.at(i).first, ArgumentList.at(i).second)); }
If possible, I do not want to explicitly define these functions, as shown below:
class MyClass { public: void LoadFunctions() { std::vector<????> MyFunctions; MyFunctions.push_back(MyFoo_00); MyFunctions.push_back(MyFoo_01); MyFunctions.push_back(MyFoo_02);
An implementation using some standard library tool (e.g. using std::function ) is fine. But a non-standard way to do this (for example, using Boost, QT or any other library or framework) is not preferred.
source share