What's the difference between metafunction classes and placeholders and higher order functions?
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