繁体   English   中英

直接从bind1st和bind2nd调用返回函子

[英]Directly call the return functor from bind1st and bind2nd

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
}

发生许多构建错误。 为什么会这样呢?

我相信一元函数对一个参数进行操作,而二元函数对两个参数进行操作。 例如

  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调用的输入类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM