What's the difference between metafunction classes and placeholders and higher order functions?
2 Answers
Boost provides features for Metafunctions and higher order functions, but the concepts are not specific to Boost.
the term "metafunction" describes a template metaprogramming technique for using template specialisation to allow the compiler to make decisions at compile time depending on their template argument.
typically, a metafunction could look something like this
template<bool B>
struct my_metafunction
{
enum { value = 1 };
};
template<>
struct my_metafunction<false>
{
enum { value = 0 };
};
When my_metafunction
is used, the exact value of my_metafunction<B>::value
will depend on the value of B (i.e. my_metafunction<true>::value
will be different to my_metafunction<false>::value
). If you're unfamiliar with template metaprogramming then you're probably wondering why this is useful - and the reality is that it's generally only useful for people writing libraries which heavily use templates and compile-time decision making. (template metaprogramming is a whole different paradigm!)
on the other hand, "higher order function" describes a functional programming technique which allows you to pass functions as function arguments. This is a little easier to express in standard C++ with the standard <algorithm>
library working with standard containers.
for example, C++ includes a higher-order function called transform
- its purpose is to step through each element in a container (such as a vector, string, list, map, array, etc) and perform a transformation for each element.
std::string str = "Hello, World";
std::transform(str.begin(),
str.end(),
str.begin(),
std::toupper); // Note - toupper is a function!
std::cout << str << std::endl;
The transformation performed is dependent on its final parameter, std::toupper
. the purpose of std::toupper is to accept a (character) value and return the uppercase version of that value. std::transform grabs the result of toupper for each element between str.begin()
and str.end()
(and in this example, the result gets put back into str)
There are plenty of other examples of higher-order functions in the standard library - std::find_if
, std::sort
, std::count_if
, to name a few. Higher-order functions in C++ can typically accept any kind of callable object, including a function, a lambda or a function object.
The callable objects which are passed are often referred to as [i]predicates[/i] (Although I'm not totally sure if that's the correct usage of the term "predicate")
the Boost placeholders are part of another aspect of functional programming known as currying - which allows you to 'bind' parameters to a function before that function is called. (in the world of Boost, the outcome of currying is a function-object which is usually passed to a higher-order function).
Currying is intended as an alternative to writing your own custom specialised callable object, by reusing existing callable objects/predicates and setting some of their arguments in concrete before they're used.
For example, you could use the higher-order-function find_if
to search a string for the first character which is "greater than q". the C++ standard library includes a callable object called greater_equals
, except it needs a second parameter to be used by find_if ("Greater than and equals **to what??")
Without currying, you might write a function (ignore all case-sensitivity for now) such as
bool greater_than_or_equals_to_q(char c)
{
return c >= 'Q';
}
with currying, you could "bind" the character 'Q` to the greater_equals function in order to create a predicate which only accepts a single argument, and represents a predicate yielding true when its argument is "greater than or equal to Q".
std::bind( std::greater_equal<char>(),
std::placeholders::_1,
'Q' );
So, using the uppercase string:
std::string str = "HELLO WORLD";
auto gt_eq_Q = std::bind( std::greater_equal<char>(),
std::placeholders::_1,
'Q' );
auto iter = std::find_if( str.begin(), str.end(), gt_eq_Q );
std::cout << *iter << std::endl;
The output, as expected - the first character in "HELLO WORLD" which is greater-than-or-equal-to 'Q' happens to be 'W'
W

- 5,741
- 1
- 27
- 34
-
Possibly the wrong placeholders the OP wants to know about. Boost.MPL has higher-order metafunctions (although the OP did mention functions), like `less<_1, _2>`. [Relevant docs](http://www.boost.org/doc/libs/1_49_0/libs/mpl/doc/tutorial/tutorial-metafunctions.html). – Luc Danton Apr 22 '12 at 01:33
-
2A "predicate" is function that evaluates a condition on some object (or objects), returning whether the condition is true or not. As such it's reasonably common for higher-order functions to take predicate functions as arguments (e.g. a filter function that returns all elements of a collection that meet some condition would take a function to test the condition, so you don't have to rewrite the filter function for every possible condition). So a predicate is basically a particular *kind* of thing you pass to a higher-order function; it's not correct to always call them predicates. – Ben Apr 22 '12 at 12:19
For detailed explanations have a look at
In principle a metafunction is either:
- a class template in which all the parameters are types
- a class with publicly accessible type
type
For example:
template <bool, class L, class R>
struct IF
{
typedef R type;
};
template <class L, class R>
struct IF<true, L, R>
{
typedef L type;
};
A function that operates on another function is known as a higher-order function. So, higher-order metafunction is a metafunction that accepts other metafunctions as parameters and use them during computation. This is conceptually similar to a function accepting a pointer to another function or a function object as a parameter at run-time. Only difference is that metafunctions exist only at compile-time. boost::mpl::transform
is an example of such a higher order metafunction.

- 18,162
- 2
- 41
- 64