Higher order functions in C++

Overview:

In C++, functions are not considered as first class citizens – meaning that they cannot be passed to other functions as it is. For example – the following is perfectly valid in a functional language like Haskell, where functions are already first class citizens.


predicate t | t > 0 = True
predicate t = False

addn x y = x+y
subn x y = x-y

The code is mostly self explanatory – there is a predicate function which returns True if value greater than 0, and False otherwise. Then there is a addn and a subn function for adding and subtracting two numbers.

 predicate (addn 5 6)
 predicate (subn 5 6)

We call the predicate function while passing a function as its parameters – the passed function gets evaluated and the corresponding value is checked by predicate which returns True or False.

Right – that was easy.

Passing functions to other functions in C++: The old way

In good old days – the best way to do this was by using function pointers. Define a function pointer, and then pass it into another function where you can execute them.

#include <iostream>

typedef int (*type1)(char);
typedef int (*type2)(int);

int bar(int x) {
  std::cout << "\nthis is " << x;
}

int bar1(char ch) {
  std::cout << "\nthis is " << ch;
  return ch;
}
int foo1(type1 x, char a) {
  x(a);
}

int foo2(type2 x, int a) {
  x(a);
}
int main() {
  
type1 x = bar1;
type2 y = bar;
  
foo1(x, 'a');
foo2(y,5);

return 0;
}

We typedef the signature of the function pointer, and then pointer is assigned a function. Then it can be freely passed around to any other function. In the above example, we typedef the signature of functions bar and bar1 as type1 and type2. They are then passed around to foo1 and foo2 functions, where they are executed.

One big disadvantage with the function pointers is that they require runtime lookup – so the runtime is increased [ unless – of course – the compiler somehow magically manages to inline it ]. And yet another problem with them is that branch prediction does not work well when there are a lot of function pointers.

So we move on.

Passing functions to other functions in C++: The new way

Actually – I lied – there isn’t a ‘new way’ – instead there are many new ways now. One of these methods is to simply create a functor and pass it over to a function.

Another method is to use boost::bind [ std::bind, if you have C++0x ]. bind can be used to bind arguments to a function – and create a function object from it. So, the above example can be rewritten using boost::bind as

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>


int bar(int x) {
  std::cout << "\nthis is " << x;
}

int bar1(char ch) {
  std::cout << "\nthis is " << ch;
  return ch;
}

int foo1(boost::function<int (char)> x, char a) {
  x(a);
}

int foo2(boost::function<int (int)> x, int a) {
  x(a);
}

int main() {

foo2(boost::bind(bar, _1) , 1);
foo1(boost::bind(bar1, _1) , 'b');
std::cout << "\n";

return 0;
}

That’s it for now.

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s