First Class Functions in C++

I’ve been working on a library that allows you to treat functions as first class objects in C++. Meaning you can assign to a function. Here’s a download link, and here’s a very simple example:

#include "fcf.h"
void foo()
{
    std::cout << "foo\n";
}
void bar()
{
    std::cout << "bar\n";
}
void main()
{
    foo(); // prints "foo"
    auto oldFoo = fcf::function(&foo);
    FCF_ASSIGN(&foo, &bar);
    foo(); // prints "bar"
    FCF_ASSIGN(&foo, oldFoo);
    foo(); // prints "foo"
}

If you have ever worked in a language with first class functions, you’ll know that you can do some quite cool stuff with this.

Read the rest of this entry »