In the previous recipes, we saw some examples of boost::bind
usage. It may be a useful tool in pre-C++11 word, but it is hard to store boost::bind
result as a variable in C++03.
#include <functional> #include <boost/bind.hpp> const ??? var = boost::bind(std::plus<int>(), _1, _1);
In C++11, we can use auto
keyword instead of ???
and that will work. Is there a way to do it in C++03?
A knowledge of the C++11 auto
and decltype
keywords may help you to understand this recipe.
We will need a Boost.Typeof
library for getting a return type of expression:
#include <boost/typeof/typeof.hpp> BOOST_AUTO(var, boost::bind(std::plus<int>(), _1, _1));
It just creates a variable with the name var
, and the value of the expression is passed as a second argument. The type of var
is detected from the type of expression.