boost::function to encapsulate function pointers.
1. function
#include <boost/function.hpp>
#include <iostream>
#include <cstdlib>
#include <cstring> int main()
{
boost::function<int(const char*)> f = std::atoi;
std::cout << f("") << std::endl;
f = std::strlen;
std::cout << f("") << std::endl;
return ;
}
boost::function makes it possible to define a pointer to a function with a specific signature. Example above defines a pointer f that can point to functions that expecct a parameter of type const char* and return a value of type int. Once defined, functions with matching signatures can be assigned to the pointer. Please note that types do not need to match exactly. Even though std::strlen() uses std::size_t as its return value, it can still be assigned to f.
Because f is a function pointer, the assigned function can be called using operator(). Depending on what function is currently assigned, either std::atoi() or std::strlen() is called.
Assignint nullptr to a function pointer of type boost::function releases any currently assigned function. Calling it after it has been released will result in a boost::bad_function_call exception being thrown. To check whether or not a function pointer is currently assigned to a function, you can use the member functions empty() or operator bool.
2. bind a class member function to boost::function
#include <boost/function.hpp>
#include <functional>
#include <iostream> struct world
{
void hello(std::ostream &os)
{
os << "Hello, world!\n";
}
}; int main()
{
boost::function<void(world*, std::ostream&)> f = &world::hello;
world w;
f(&w, std::ref(std::cout));
return ;
}
When calling such a function, the first parameter passed indicates the particular object for which the function is called. Therefore, the first parameter after the open parenthesis inside the template definition must be a pointer to that particular class. The remaining parameters denote the signature of the corresponding memebr function.
3. boost::bind()
#include <boost/bind.hpp>
#include <vector>
#include <algorithm>
#include <iostream> void print(std::ostream *os, int i)
{
*os << i << std::endl;
} int main()
{
std::vector<int> v{, , };
std::for_each(v.begin(), v.end(), boost::bind(print, &std::cout, _1));
std::sort(v.begin(), v.end(), boost::bind(compare, _1, _2));
return ;
}
Example uses print() as a function, not as a function object. Because print() expects two parameters, teh function can't be passed directly to std::for_each(). Instead, boost::bind() is passed to std::for_each() and print() is passed as the first parameter to boost::bind(). _1 is a placeholder. Boost.Bind defines placeholders from _1 to _9. These placeholders tell boost::bind() to return a function object that expects as many parameters as the placeholder with the greatest number. boost::bind() returns an unary function object - a function object that expects a sole parameter.
4. Boost.Ref provides two functions, boost::ref() and boost::cref(). Because std::bind() takes parameters by value, you have to deal with references explicityl.
#include <boost/ref.hpp>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream> void print(std::ostream &os, int i)
{
os << i << std::endl;
} int main()
{
std::vector<int> v{, , };
std::for_each(v.begin(), v.end(), std::bind(print, boost::ref(std::cout), std::placeholders::_1));
return ;
}
boost::ref() is used to wrap std::cout. boost::ref() returns a proxy object that contains a reference to the object passed to it. This makes it possible to pass a reference to std::cout even though std::bind() takes all parameters by value.
The function template boost::cref() lets you pass a const reference.