How to use Boost.Function

Specifying variables of function pointer type in standard C++ has a downright filthy syntax and rightly strikes confusion into the hearts of programmers everywhere. Boost.Function provides a different syntax which is far superior in every way. This tutorial shows how to use Boost.Function with examples of common tasks and covers all of the main features of the library.

Please note that Boost.Function has a sister library that it should be used in conjunction with; this is Boost.Bind. In a nutshell Boost.Function allows you to create variables of function pointer type and Boost.Bind allows you to dynamically bind a function into a function pointer variable.

If you are going to work with function pointers then make sure you get to know both Boost.Function and Boost.Bind as they are orders of magnitude better than using just standard C++.

Declaring a boost::function variable

boost::function is the central type in Boost.Function. Declaring a variable of this type is straightforward and the general form of a declaration is shown below. The crux of the declaration is specifying the return type and parameters for the function pointer.

boost::function<RETURN_TYPE(PARAM_TYPE_1, PARAM_TYPE_N)> boostFunction;

Example Declarations

boost::function<void()> voidFunction; // returns void and
                                      // takes no parameters 
boost::function<int(int, char**)> mainFunction; // same signature as 
                                                // standard main() 
boost::function<void(std::string)> stringFunction;  // returns void and
                                                    // takes a string

Checking if a boost::function variable can be called safely

A boost::function either contains a valid callable function pointer or it doesn't. It is important that you don't try to call a boost::function if it does not contain a valid function pointer as the result is undefined behaviour. See below for how to check the validity of a boost::function variable.

boost::function<void()> boostFunction; 

if ( boostFunction ) // contains valid method

if ( boostFunction.empty() ) // does NOT contain valid method

Calling a boost::function variable

Executing a variable of boost::function type calls the contained method. To achieve this boost::function overrides the () operator. So executing one is intuitive, see below for examples.

// some variables
int number = 0;
std::string aString("text");
float aFloat = 0.f;

// some function pointers
boost::function<void()> voidFunction;
boost::function<int(std::string)> stringAndIntFunction;
boost::function<void(float, int)> floatAndIntFunction;

// call the functions
number = stringAndIntFunction(aString);
floatAndIntFunction(aFloat, number);


If you've noticed that a fundamental part of the process of using Boost.Function has been omitted so far then you would be right. The best practice way of creating function pointers that can be stored in boost::function objects is through Boost.Bind. With that in mind please proceed to the Boost.Bind page here for further detail on how to create function pointers.

10 Responses to “How to use Boost.Function”

  1. This topic is very helpful.It removes all my confusion regarding syntaxes of boost::function/bins thanks you very much. Boost site is not that user friendly. Please write more article on boost like threading/multi-threading etc.

    October 23rd, 2011
    • Glad you found it helpful 🙂

      November 8th, 2011
  2. Your tutorials on boost are the best on the internet. Period. Please write a book, I will buy it.

    December 10th, 2011
    • Thanks, I’ll see what I can do about writing some more 🙂

      December 10th, 2011
  3. Nice tutorial. This brings things down to the point! Thanks a lot.

    Michael Nett
    December 28th, 2011
  4. Thanks for the clear explanation. I needed to store function objects returned by boost::bind(), and now I know how to do that!

    Joe VanAndel
    April 27th, 2012
  5. I will buy your book too!

    March 12th, 2014
  6. Ditto on the book! Question: If you want to pass a boost function as a variable, can you default its value to something that won’t pass the valid function test?

    July 30th, 2014
    • Hi Dex,

      A default constructed boost::function object will evaluate false if used in a test e.g.

      boost::function invalid;
      if ( ! invalid ) { // do stuff }

      Is that what you were asking?


      Just realised there’s a more direct method. All you need to do is assign it to NULL (or nullptr for std::function) e.g.

      void func(boost::function invalid = NULL)
      if ( ! invalid ) { // do stuff }

      July 30th, 2014
  7. Good explanation.. Thanku very much..

    October 16th, 2018

Leave a Comment