Partial function applications

Partial function application is the process of taking a function with a number of arguments, fixing (or binding) some of its arguments and producing a new function with a smaller number of arguments. In C++, this can be achieved with std::bind() that generates a forwarding call wrapper for a callable object, and the placeholder objects from the std::placeholders namespace that are used for binding arguments to the callable object.

To see how this works, let’s start with the following function that is supposed to print a document to a specified printer with various settings. These include page orientation and size, number of copies, size of margins, resolution, and scale. Some of these have default values because in most cases you’d use the same values for these arguments.

If you want to print to a printer called “Printer” one copy on an A4 page in portrait layout, with standard margins, 300 DPI and no scaling, then you make the following call, relying on the default values.

Should you need two copies of the document, you need to explicitly provide that argument in the call.

What if you need to print with a different resolution, such as 600 DPI instead of 300? In C++, arguments are positional, they need to be supplied in the order defined by the function declaration. Other languages, such as C#, support named arguments so that you can provide arguments in any order given that you specify the name of the parameter. Because of this, however, when you need to specify other value than the default for the DPI, you also need to specify values for the number of copies and the margins. In other words, your call would look like this.

This is where std::bind() can step in. Default arguments for function parameters and binding are different things, but the effects are somehow similar. For instance, if all we need to change is the page size and margins, we can leverage a partial function application on the original function (that may also have default arguments) and create a callable forwarding wrapper with placeholders for the parameters we need to supply on every call and fix values for the others. Placeholders are objects in the std::placeholders namespace called _1, _2, …, _N (where N is implementation defined), whose type is also an implementation detail (although it must be default and copy-constructible).

In this example _1 and _2 are objects that are stored in the function object generatated by std::bind(). When the function object is invoked with arguments, each placeholder is replaced by the corresponding unbound argument (_1 by the first argument, _2 by the second, etc.). One thing to notice is that when creating a callable wrapper with std::bind() you must supply values even for the parameters with default arguments.

Of course, you’re not going to create a binding for making one or two calls like that. But you may have situations when one function with multiple arguments could be called repeatedly within the same routine or algorithm with only some of them changed. In this case you can use std::bind().

It is also useful when you want to use a function with multiple arguments with a standard algorithm that expects fewer arguments than the function requires. In the following example, the print() function is used to print the same document to multiple printers. std::for_each is used for this purpose, although the only argument that is supplied to the function is the printer name. Therefore, a callable wrapper is created with std::bind().

In the examples so far, the print() function was a free function, but the process works the same with member functions too. In the following example, print() is a member of the document class.

To invoke this member function on a particular instance of the class you must suply the object as an argument to std::bind(); the rest is the same.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.