C++:绑定的结合?

Gio*_*hal 7 c++ boost bind

假设有以下两个函数:

#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);
}
Run Code Online (Sandbox Code Playgroud)

这些函数中的每一个都有两个参数,但可以转换为一个可调用的对象,它只使用一个参数(std/boost)bind.有点像:

boost::bind(match1, "a test");
boost::bind(match2, 42);
Run Code Online (Sandbox Code Playgroud)

我希望能够从两个bool带有一个参数并返回的函数中获取一个可调用的对象,它接受两个参数并返回bools 的&&.参数的类型是任意的.

operator&&返回的函数一样bool.

Jes*_*der 9

返回类型的boost::bind重载operator &&(以及许多其他).所以你可以写

boost::bind(match1, "a test", _1) && boost::bind(match2, 42, _2);
Run Code Online (Sandbox Code Playgroud)

如果要存储此值,请使用boost::function.在这种情况下,类型将是

boost::function<bool(const char *, const char *)>
Run Code Online (Sandbox Code Playgroud)

请注意,这不是boost::bind(未指定)的返回类型,但具有正确签名的任何仿函数都可以转换为boost::function.