【发布时间】:2010-04-14 15:35:36
【问题描述】:
假设有以下两个函数:
#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);
}
这些函数中的每一个都接受两个参数,但可以通过使用 (std/boost)bind 将其转换为只接受一个参数的可调用对象。大致如下:
boost::bind(match1, "a test");
boost::bind(match2, 42);
我希望能够从像这样的两个接受一个参数并返回 bool 的函数中获取一个可调用对象,该对象接受两个参数并返回 bools 的 && .参数的类型是任意的。
对于返回 bool 的函数,类似于 operator&&。
【问题讨论】:
-
你不想要一个接受一个参数并返回一个 bool 的函数吗?即相当于
match1("a test", X) && match2(42, X)?还是你真的想要match1("a test", X) && match2(42, Y)? -
我真的想要一个带有两个参数的函数。