Skip to content
Search

C++:Functor

lastmod 2015-03-19created 2015-03-193 edits

An STL functor (Functor), also called a function object, means a function object: an object that can be used like a function with operator().

Syntax

It can be written as follows.

class FunctorClass
{
public:
    return-type operator () ( parameters... )
    {
        // do something...
    }
};

Pros

Functors have the following three advantages.

  • A functor can have state. That is, the user can have different states for the same functor at the same time.
  • A functor has its own type. In short, it can be used as a type. This means a functor can be used as a template argument, as a type.
  • A functor is faster than a function pointer (FunctionPointer).1

Favorite site

References