(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Function Objects

Description: Passing functions around as typed class objects.

Keywords: ecl function objects

Tutorial Level: INTERMEDIATE


Resolving c++ function passing (as arguments to other functions) is complicated by the differences between global/static and member functions as well as by the awkward syntax. Here we attempt to provide a standardised approach to using them.


One way of standardising the approach is to utilise the idea of a function object (also known as functor). A function object is simply an object that characterises a function. In c++, this has three advantages:

  • Function code can be inlined (instead of repeated pointer calls) when used repetitively in a loop.
  • The function object can maintain state, something which pure function callbacks cannot do.
  • The syntax is much cleaner and more flexible.

An example of a unary function object is given below:

   1 class Sum {
   2     int val;
   3 public:
   4     Sum(int i) :val(i) { }
   5     int operator()(int i) { return val+=i; }        // unary function
   6 };

We also make a terminology distinction here when distinguishing between free and member functions. Free functions are defined to be global or static functions.

Compiling & Linking

Include some or all of the following at the top of any translation unit that requires compilation of class that uses parameters.

   1 #include <ecl/utilities.hpp>
   3 // The classes
   4 using ecl::NullaryFunction;
   5 using ecl::UnaryFunction;
   6 using ecl::BinaryFunction;
   7 using ecl::NullaryFreeFunction;
   8 using ecl::UnaryFreeFunction;
   9 using ecl::BoundUnaryFreeFunction;
  10 using ecl::NullaryMemberFunction;
  11 using ecl::BoundNullaryMemberFunction;
  12 using ecl::UnaryMemberFunction;
  13 using ecl::PartiallyBoundUnaryMemberFunction;
  14 using ecl::BoundUnaryMemberFunction;
  15 using ecl::NullaryFunctionCopy;
  16 using ecl::NullaryFunctionReference;
  17 using ecl::UnaryFunctionCopy;
  18 using ecl::UnaryFunctionReference;
  20 // Overloaded utility functions
  21 using ecl::generateFunctionObject;

Since it is a template class, no linking is required if you are only using this class.


Many of the higher level classes in the ecl utilise function objects, e.g. ecl_threads, ecl_sigslots. These classes expect certain concepts to be fulfilled when accepting function objects as arguments and also make use of some of the convenience classes/tools in the above list.


Classes that accept function objects generally utilise a template parameter and require the function object to fulfill the requirements of a concept. For example, threads require function objects to satisfy the nullary function concept. The current list of concepts for function objects include:

  • NullaryFunction

Documentation for the concepts can be found in the ecl_concepts package.

Creating Your Own Function Objects

When constructing your own function object classes to be used with higher level ecl components, they must conform to the requirements of their target concept. For example, a suitable thread class function object:

   1 class ThreadFunction {
   2 public:
   3     typedef void result_type;
   4     void operator()() {
   5         // thread worker function
   6     }
   7 };
   8 ThreadFunction thread_function;
   9 Thread thread(thread_function); // Alternatively Thread thread = Thread(ThreadFunction());

Wrapping Free/Member Functions

Wrapping functions can be done via construction calls to many of the classes listed above, however to make it easier, there is the overloaded ecl::generateFunctionObject method.

A good example of its usage is with the ecl Thread class where a nullary function object is required for construction.

   1 int f(int i) {}
   2 class A {
   3     void f() {}
   4     void g(int i) {}
   5 };
   6 // ...
   7 A a;
   8 Thread thread0(generateFunctionObject(f, 3));         // Bind the first argument to a global function
   9 Thread thread1(generateFunctionObject(&A::f, a));     // Bind an object instance with the nullary function
  10 Thread thread2(generateFunctionObject(&A::g, a, 2));  // Bind object instance, first argument to a unary member function

For member functions, in the above code we bound the instance with the member function. Alternatively, you can leave it free so that the following two lines of code produce the same result:

   1 generateFunctionObject(&A::f, a)(); // This case produces and calls a nullary function
   2 generateFunctionObject(&A::f)(a);   // This case produces and calls a unary function

Case Scenario : Threads

Threads uses the tools here, an example of thread function loading is shown below.

   1 A a;                             // Just an ordinary class with nullary member f().
   2 NullaryFunction function_object; // Conforms to the concept above.
   3 Thread thread_f1(f);
   4 Thread thread_f2(&A::f,a);
   5 Thread thread_f3(generateFunctionObject(f));
   6 Thread thread_f4(generateFunctionObject(g,1));
   7 Thread thread_f5(generateFunctionObject(&A::f,a));
   8 Thread thread_f6(generateFunctionObject(&A::g,a,2));
   9 Thread thread_f7(function_object);
  10 Thread thread_f8 = NullaryFunction();
  11 Thread thread_f8(ref(function_object));


We only utilise at most one argument to any free/member function. In practice a limit has to be drawn somewhere and I find you can always bundle however many arguments you wish into an appropriately defined structure, so either none or one is always sufficient for all purposes.

Wiki: ecl_utilities/Tutorials/Function Objects (last edited 2012-02-01 14:01:01 by DanielStonier)