1

是否可以?该指令bool b = (boost::bind(func, 1) == boost::bind(func, 1))无法编译,因为它“无法从 'boost::_bi::bind_t' 转换为 'bool'”。(的签名funcvoid func(int)。)

4

2 回答 2

3

Boost.Bind 重载关系运算符以返回嵌套的绑定表达式。因此,在您的代码中boost::bind(func, 1) == boost::bind(func, 1)返回一个空值(因为您的绑定表达式中没有占位符)函子,当被调用时,返回func(1) == func(1). 这是谓词的一个方便特性,除其他用途外:

typeded std::pair<T, U> pair_type;
// find pair where the first element is equal to 3
std::find_if(begin, end, boost::bind(&pair_type::first, _1) == 3);

此外,返回的对象是不可转换的bool,这就是它无法编译的原因(忽略它没有做你想做的事情的问题)。

您想要做的不是 Boost.Bind 接口的一部分。Tt 不会是一个非常有用的功能,并且在(非常)一般情况下是undecidable

于 2011-07-30T21:02:18.003 回答
1

不知道这是否是“官方支持的功能”,但 bind_t 似乎确实提供了一种function_equal方法: http: //www.boost.org/doc/libs/1_47_0/boost/bind/bind.hpp

于 2011-07-30T20:36:22.477 回答