直接从bind1st和bind2nd中调用返回函子
Directly call the return functor from bind1st and bind2nd
bind1st和bind2nd的返回值源自一元函数。通过调用它们,我认为它们提供了一个接受一个参数的函数对象。但这可能是错误的。
这是我的密码。
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;}
是一个二进制函数。
更改模板(并考虑不使用领先的欠交叉):
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
调用的输入类型。
相关文章:
- 如何安全地从 DLL 调用返回对象
- 类对象属性是否可以调用返回属性应具有的值的方法C++?
- 从类似构造函数的调用返回模板化智能指针
- C ++ DLL导入:函数调用返回错误的结果
- 如何调用返回类方法指针的类方法
- 如何调用返回双精度或字符串并将其保存在地图上但作为对象的类的方法
- FindResource 调用返回 RCDATA 类型的文本文件的 NULL
- 如何调用返回指针数组的函数?C++
- Perl xs - 无法从方法调用返回新的自定义 C++ 对象 - 改为返回标量值
- BOOST ::线程内会成员功能调用返回unique_lock Instantiation错误
- 断言调用返回的函数的签名 &(引用)
- 对于相同的输入参数,多个函数调用返回不同的结果
- 如何调用返回后删除[]内存
- 使用二维数组调用返回结构
- C++:声明、定义和调用返回模板类对象的函数
- 调用返回数组的函数
- c++方法从c#dllimport调用返回0
- 方法调用返回空值
- 我希望计时器只在上一个调用返回或完成执行时调用代码
- 任何时间函数保证为调用返回不同的值