4

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
hkBattousai
  • 10,583
  • 18
  • 76
  • 124

4 Answers4

6

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.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • Boost.Lambda has been officially deprecated since [Boost.Phoenix](http://www.boost.org/libs/phoenix/) v3 was released in Boost 1.47. Please recommend Phoenix for new code rather than Lambda. – ildjarn Dec 27 '11 at 19:40
3

Assuming your compiler is modern enough, you can use the new std::function type and anonymous (lambda) functions introduced in C++11:

std::vector<std::function<double(int, float)>> MyFunctions;
MyFunctions.push_back([](int n, float f) {
    return (double) f / (double) n;
});
MyFunctions.push_back([](int n, float f) {
    return (double) sqrt((double) f) / (double) n;
});
// ...
MyFunctions.push_back([](int n, float f) {
    return (double) (f * f) / (double) (n + 1);
});
Xion
  • 22,400
  • 10
  • 55
  • 79
  • 3
    You don't even have to use `function` here (unless you're using VC2010). Captureless lambda gracefully degrade into function pointers. However, it's always nice to use `function`, in case you want to use something other than a function pointer. – Nicol Bolas Dec 27 '11 at 19:15
2

You can do this using std::function and lambdas:

#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
#include <iterator>

struct dispatcher {
  template <typename F, typename Pair>
  double operator()(const F& func, const Pair& p) const {
    return func(p.first, p.second);
  }
};

int main() {
  std::vector<std::function<double(int,double)>> functions;
  functions.push_back([](int n, float f) { return double(f)/double(n); });

  std::vector<std::pair<int, float>> args = {std::make_pair(1, 10.0f)};

  std::vector<double> results;

  std::transform(functions.begin(), functions.end(), args.begin(), std::back_inserter(results), dispatcher());

  std::copy(results.begin(), results.end(), std::ostream_iterator<double>(std::cout, "\n"));
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
0

Function pointers are fairly enough, no need to use even std::function:

#include<iostream>
#include<vector>
#include<cmath>

int main()
{
      std::vector<double (*)(double)> vec;
      vec.push_back([](double x) {return cos(x);});
      vec.push_back([](double x) {return sin(x);});
      vec.push_back([](double x) {return tan(x);});
      for (auto f: vec)
          std::cout<<f(M_PI/4)<<'\n';
      return 0;
}
Alexey Godin
  • 307
  • 1
  • 6