0

I did overload the operator() for my functor that now it receives two params. I know it possible, but how I invoke it? as far as I see, functions as std::transform or std::for_each iterate over single param each time.

minimal example:

struct Functor
{
      Functor(double epsilon, double delta): ...
      float operator()(int a, int b) const;

      private:
      _epsilon, _delta;
}

Functor f(.1, .2);
std::vector v = {1, 2, 3, 4}; // a should be taken from here each time
int b = 10;
std::vector k; // save result into k
std::transform(v.begin(), v.end(), b, k, f);

thank you!

edit: I do it because I want a to be taken each time from v, whereas b will be constant

Nissim Levy
  • 165
  • 10

1 Answers1

2

You don't need a standard algorithm to call a functor. You can use the function call operator and pass as many arguments as the functor expects:

Functor{.1, .2}(1, 2);

You've simply chosen two standard algorithms that use a unary functor as your examples. There are other standard algorithms that use functors of different arity. For example, std::find uses a binary functor such as the one in your example.

I want ... b ... be constant

This is what functors are for. They can be composed. Here is an example of creating a unary functor from your binary one. I'll use a lambda:

auto unary = [](int a) {
    return Functor{.1, .2}(a, 2);
};
eerorika
  • 232,697
  • 12
  • 197
  • 326