您可以简单地否定返回值。
bool Bar::filterFoo(Foo& foo)
{
return (std::find_if(filters.begin(), filters.end(), !boost::lambda::bind(boost::lambda::_1, foo)) == filters.end());
}
或者您可以使用 c++0X 中的 lambda
bool Bar::filterFoo(Foo& foo)
{
return (std::find_if(filters.begin(), filters.end(), [&foo](filter_function& f){
return !f(foo);
}
) == filters.end());
}
展示一个至少适用于 VS2010 的完整示例。
#include <iostream>
#include <vector>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/bind.hpp>
using namespace std;
struct Foo{};
typedef boost::function<bool (Foo)> filter_function;
std::vector<filter_function> filters;
static int g_c = 0;
bool MyFunc(Foo /*foo*/)
{
if(g_c > 1)
return true;
g_c++;
return false;
}
bool filterFoo(Foo& foo)
{
return (std::find_if(filters.begin(), filters.end(), boost::lambda::bind(boost::lambda::_1, foo)) == filters.end());
}
bool negatefilterFoo(Foo& foo)
{
return (std::find_if(filters.begin(), filters.end(), !boost::lambda::bind(boost::lambda::_1, foo)) == filters.end());
}
int main()
{
Foo f;
filters.push_back(boost::bind(&MyFunc, _1));
filters.push_back(boost::bind(&MyFunc, _1));
filters.push_back(boost::bind(&MyFunc, _1));
std::cout << filterFoo(f) << std::endl;
std::cout << negatefilterFoo(f) << std::endl;
return 0;
}
它在我的机器上返回 0 和 1。