Use the std::tr1::function Class to Generalize Callbacks

Use the std::tr1::function Class to Generalize Callbacks

or a C++ programmer, the term “a callback function” means at least three different things: a pointer to a freestanding function (i.e., a C function), a pointer to a member function, or a function object. However, in high level programming, these technical minutiae just get in the way. What you need is a generalized abstraction mechanism that can encapsulate any callable entity regardless of its underlying implementation. The recently standardized std::tr1::function class template offers exactly that. It’s a generic callback mechanism with intuitive and uniform syntax and semantics. This article will show you how to use this class template.

How do you generalize the concept of callbacks while maintaining similar syntax and semantics to function pointers?

Use std::tr1::function class template as a generic wrapper for all callable entities.

The recently standardized Library Extension Technical Report 1 (TR1) includes a new class template called std::tr1::function that generalizes the notion of a callable entity while mimicking pointers to functions in both syntax and semantics. std::tr1::function can wrap any function object, pointer to function or pointer to a member function that can be called with the same arguments and return types.

A declaration of an std::tr1::function object includes the return type and the arguments of the target function:

#include using std::tr1::function;function  func;

You can assign to func any callable entity that returns long and takes two arguments of type int:

long add(int x, int y) { return x+y; }func = &add; //assign a target function cout << func(1, 2) << endl; // calls add(1,2) 

Unlike with ordinary pointers to functions, the return type and the arguments of the target function may differ from those of func so long as there is an implicit conversion between those types. The following code listing demonstrates this property. It binds a function whose arguments are of type long, long to a std::tr1::function object whose arguments are of type int, int:

long subtract(long x, long y) { return x-y; }func = &subtract; //OK, implicit conversion of argumentscout << func(2, 1) << endl; // output: 1

Binding a Function Object
The same std::tr1::function object may change its target, even if the original target and the new target have radically different implementations. In the following example, the object func2 is first bound to the freestanding function adjacent(). Later, the same func2 is bound to a function object:

using namespace std;bool adjacent(int x, int y){return x == y-1 || x == y+1;}class Data {private: vector  > records;public: bool operator()(int x, int y) {   records.push_back(make_pair(x, y));   return x < y; }};    function  func2;    func2 = &adjacent;func2(2, 3); // calls adjacent(2, 3), returns true    Data rec;func2 = ref(rec); //see note func2 (5, 2)); // adds  pair<5, 2> to rec.records
Author's Note: The std::tr1::function specification makes use of another TR1 feature called reference wrapper classes. I will discuss reference wrappers in a future 10-Minute Solution, but for the time being, assume that ref() is a standard helper function that takes an argument of type X and returns an object whose interface simulates a X& object.

You may also assign two std::tr1::function objects whose return types and arguments are different if there is an implicit conversion between those types:

func2=func;//OK, long return value is convertible to bool

Implicit Boolean Conversion
std::tr1::function defines a Boolean conversion operator that enables you to use objects of this type in Boolean expressions:

std::tr1::function f) f;assert (!f); //OK, f is emptyf=&add;assert (f); //OK, f isn't emptyvoid func(function ) &f1);{ if(f1) {  f1(2,3); } else {  f1=&subtract;  f1(3,2); }}

Higher Order Functions
Higher-order functions are a key concept in the functional programming paradigm. A higher order function accepts functions as its argument and returns a function as its result. std::tr1::function facilitates the implementation of higher order functions. Consider a calculator program that accepts an operation code from the user and returns a matching function in the form of a std::tr1::function object:

#include using std::tr1::function;function  //return type  get_operation(char opcode){ switch (opcode) {  case '+': return std::plus();  case '-': return std::minus();  case '/': return std::divides();  case '*': return std::multiplies();  case '%': return std::modulus();  default: return function(); }}

The use of std::tr1::function has two advantages: a function object has a state, unlike a bare pointer. Additionally, it simplifies the syntax considerably.

A Useful Improvement
std::tr1::function provides a generic, standardized and intuitive mechanism for encapsulating the low-level details of callbacks. As opposed to typedefs, virtual function overriding and classic function objects, this new class has relaxed type matching rules, thus offering more flexibility while reducing the amount of boilerplate code required by other encapsulation techniques.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist