1

Let's take a simple functor

class LessThan {
public:
    explicit LessThan (int in ) : val(in) {}
    bool operator() (int param) const {
        return param < val;
    }
private:
    int val;
};

which I can use like this for example -

vector<int> myVector = factory();
size_t count = count_if(begin(myVector), end(myVector), LessThan(10));

My understanding of the terminology surrounding functors is that the class LessThan is a functor. i.e. it is a type, not an object. i.e it is abstract, not concrete.

So, what do we call LessThan(10)? Here, we are creating an object by instantiating the LessThan functor type. So, do we call it a functor object? But, functors are function objects. So, LessThan(10) is a function object object??

Isn't that absurd?

The Vivandiere
  • 3,059
  • 3
  • 28
  • 50
  • Objects *are* types. Or rather, they are *instances* of types. User defined classes are types and when you instantiate them you get objects of that type. – Jesper Juhl Jul 02 '16 at 18:09
  • 2
    @JesperJuhl For `int a`, ...`int` is a type and `a` is an object. And the two are thus different. – The Vivandiere Jul 02 '16 at 18:11
  • 1
    If it helps, just think of it this way: `LessThan` is a _function object type_. `LessThan(10)` is a _function object_. – underscore_d Jul 02 '16 at 18:27

1 Answers1

1

Terminology for function objects is the same as terminology for regular objects: your functor LessThan(10) has a type, which is class LessThan.

Note that your functor is a combination of a function (i.e. "less than") and one of its arguments (i.e. int val). The same effect can be achieved through functor composition:

auto f = bind(less<int>(), _1, 10); // f is LessThan(10)

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @typetraitor That's correct. A lot of functor types represent pure functions, and thus have no state (e.g. `std::less`, `std::plus`, etc.) This makes it much easier to confuse the type and the object, because all objects of the type are essentially the same. – Sergey Kalinichenko Jul 02 '16 at 18:25