简体   繁体   English

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

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

The return values of bind1st and bind2nd are derived from unary_function. bind1st和bind2nd的返回值是从unary_function派生的。 By calling them, I think they provide a function object that accepts one argument. 通过调用它们,我认为它们提供了一个接受一个参数的函数对象。 But this maybe is wrong. 但这也许是错误的。

Here is my code. 这是我的代码。

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
}

A lot of build errors occur. 发生许多构建错误。 Why is this wrong? 为什么会这样呢?

I believe a unary function operates on one parameter, while a binary function operates on two. 我相信一元函数对一个参数进行操作,而二元函数对两个参数进行操作。 For example 例如

  T operator()(T arg1, T arg2) const {return arg1 + arg2;}

is a binary_function. 是binary_function。

Change the template (and consider not using leading underscroes): 更改模板(并考虑不使用前导下划线):

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;}
};

So, fun is a binary functor. 因此, fun是一个二进制函子。 After you bind one of its arguments, eg by calling std::bind2nd(func<int>(),10) you will then have a unary function. 您绑定它的一个参数,例如,通过调用std::bind2nd(func<int>(),10)然后你会拥有一个一元函数。 This will not alter the type of the input to the bind2nd call. 这不会更改bind2nd调用的输入类型。

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

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