Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

3.5 Negators and Binders

Negators and binders are function adaptors that are used to build new function objects out of existing function objects. Almost always, negators and binders are applied to functions as part of the process of building an argument list prior to invoking yet another function or generic algorithm.

The negators not1() and not2() take a unary and a binary predicate function object, respectively, and create a new function object that yields the complement of the original. For example, using the widget tester function object defined in Section, the function object

yields a binary predicate which takes exactly the same arguments as the widget tester. It is true when the corresponding widget tester is false, and false otherwise. Negators work only with function objects defined as subclasses of the class templates unary_function and binary_function, given earlier.

A binder takes a two-argument function, and binds either the first or second argument to a specific value, thereby yielding a one-argument function. The type of the underlying function object must be a subclass of binary_function. The binder bind1st() binds the first argument, while the binder bind2nd() binds the second.

For example, the binder bind2nd(greater<int>(), 5) creates a function object that tests for being larger than 5. This could be used in the following, which yields an iterator representing the first value in a list larger than 5:

Combining a binder and a negator, we can create a function object that is true if the argument is divisible by 3, and false otherwise. This function can be used to remove all the multiples of 3 from a list.

A binder is used to tie the widget number of a call to the binary function WidgetTester(), yielding a one-argument function that takes only a widget as argument. This is used to find the first widget that matches the given widget type:

NOTE -- The idea described here by the term binder is in other contexts often described by the term curry. It is named after the computer scientist Haskell P. Curry, who used the concept extensively in an influential book on the theory of computation in the 1930s. Curry himself attributed the idea to Moses Schönfinkel, leaving one to wonder why we don't instead refer to binders as Schönfinkels.

Previous fileTop of DocumentContentsIndex pageNext file