Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an std::vector of functions without defining the functions explicitly?

I want to create an std::vector object (or any other standard or custom container type) with elements of custom and arbitrary functions 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 don't want to define these set of functions explicitly as below:

class MyClass
{
    public:
        void LoadFunctions()
        {
            std::vector<????> MyFunctions;
            MyFunctions.push_back(MyFoo_00);
            MyFunctions.push_back(MyFoo_01);
            MyFunctions.push_back(MyFoo_02);
            // ...
            MyFunctions.push_back(MyFoo_nn);
        }

    private:
        double MyFoo_00(int n, float f) { /* ... */ }
        double MyFoo_01(int n, float f) { /* ... */ }
        double MyFoo_02(int n, float f) { /* ... */ }
        // ...
        double MyFoo_nn(int n, float f) { /* ... */ }
};

An implementation with some standard library tool (like using std::function) is OK. But, a non-standard way of doing this (like using Boost, QT or any other library or framework) is not preferred.

like image 427
hkBattousai Avatar asked Dec 22 '22 04:12

hkBattousai


1 Answers

It sounds like you want lambda functions. If your C++ compiler implements this part of the C++11 standard yet, you can use them directly. Otherwise you might be able to use Boost Phoenix or Boost Lambda.

like image 109
Fred Larson Avatar answered Dec 24 '22 03:12

Fred Larson