bind1st和bind2nd的返回值是从unary_function导出的。通过调用它们,我认为它们提供了一个接受一个参数的函数对象。但这可能是错的。
这是我的密码。
template<typename _T>
class fun: public std::unary_function<_T, _T>
{
public:
_T operator()(_T arg1, _T arg2) const {return arg1 + arg2;}
};
int main() {
bind2nd(fun<int>(),10)(10); //My intention is to directly call the results of bind2nd
}很多构建错误都会发生。为什么这样做不对?
发布于 2013-12-12 15:45:13
我相信一元函数作用于一个参数,而二进制函数作用于两个参数。例如
T operator()(T arg1, T arg2) const {return arg1 + arg2;}是一个binary_function。
更改模板(并考虑不使用领先的下划线):
template<typename T>
class fun: public std::binary_function<T, T, T>
// ^^^^^^--- well, it takes two parameters
{
public:
T operator()(T arg1, T arg2) const {return arg1 + arg2;}
};因此,fun是一个二元函子。在绑定了它的一个参数之后,例如通过调用std::bind2nd(func<int>(),10),您将拥有一个一元函数。这不会改变bind2nd调用的输入类型。
https://stackoverflow.com/questions/20547417
复制相似问题