C ++: connecting bundles?

Assume the following two functions:

#include <iostream> #include <cstdlib> // atoi #include <cstring> // strcmp #include <boost/bind.hpp> bool match1(const char* a, const char* b) { return (strcmp(a, b) == 0); } bool match2(int a, const char* b) { return (atoi(b) == a); } 

Each of these functions takes two arguments, but can be converted to a callable object that takes only one argument using (std / boost) bind . Something like:

 boost::bind(match1, "a test"); boost::bind(match2, 42); 

I want to be able to get from two such functions that take one argument and return bool , a callable object that takes two arguments and returns && from bool s . The type of arguments is arbitrary.

Something like operator&& for functions returning bool .

+7
c ++ boost bind
source share
1 answer

The return type boost::bind overloads operator && (as well as many others ). So you can write

 boost::bind(match1, "a test", _1) && boost::bind(match2, 42, _2); 

If you want to keep this value, use boost::function . In this case, the type will be

 boost::function<bool(const char *, const char *)> 

Note that this is not the return type of boost::bind (which is not specified), but any functor with the right signature is converted to boost::function .

+9
source share

All Articles