boost.bind

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

Purpose 用途

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions. bind does not place any requirements on the function object; in particular, it does not need the result_type, first_argument_type and second_argument_type standard typedefs.

boost::bind是标准库函数std::bind1ststd::bind2nd的一般化扩展。它支持任意的函数对象、函数、函数指针和类成员指针,它可以将特定的值绑定到任意一个参数或指定的位置。bind没有对函数对象提出任何要求,特别的,它不需要标准中定义的result_typefirst_argument_typesecond_argument_type等类型。

Using bind with functions and function pointers 将bind用于函数和函数指针

Given these definitions:

假定有如下定义:

int f(int a, int b) { return a + b; } int g(int a, int b, int c) { return a + b + c; } 

bind(f, 1, 2) will produce a "nullary" function object that takes no arguments and returns f(1, 2). Similarly, bind(g, 1, 2, 3)() is equivalent to g(1, 2, 3).

bind(f, 1, 2)生成一个“无参数”的函数对象,这个函数对象不需要参数,返回值为f(1, 2)。类似的,bind(g, 1, 2, 3)()等价于g(1, 2, 3)。

It is possible to selectively bind only some of the arguments. bind(f, _1, 5)(x) is equivalent to f(x, 5); here _1 is a placeholder argument that means "substitute with the first input argument."

你可以有选择的只绑定某几个参数。bind(f, _1, 5)(x)等价于f(x, 5); 这里_1是一个占位符,表示“替换为第一个参数”(在这里为5)。

For comparison, here is the same operation expressed with the standard library primitives:

和标准库中提供的操作原语相比较:

std::bind2nd(std::ptr_fun(f), 5)(x); 

bind covers the functionality of std::bind1st as well:

bind涵盖了std::bind1st的功能:

std::bind1st(std::ptr_fun(f), 5)(x); // f(5, x) bind(f, 5, _1)(x); // f(5, x) 

bind can handle functions with more than two arguments, and its argument substitution mechanism is more general:

bind可以处理多于两个参数的函数,而且它的参数替换机制更为通用。

bind(f, _2, _1)(x, y); // f(y, x) bind(g, _1, 9, _1)(x); // g(x, 9, x) bind(g, _3, _3, _3)(x, y, z); // g(z, z, z) bind(g, _1, _1, _1)(x, y, z); // g(x, x, x) 

Note that, in the last example, the function object produced by bind(g, _1, _1, _1) does not contain references to any arguments beyond the first, but it can still be used with more than one argument. Any extra arguments are silently ignored, just like the first and the second argument are ignored in the third example.

注意最后一个例子,bind(g, _1, _1, _1)生成的函数对象并不包含除第一个参数以外的其它参数,但是它仍然可以使用多个参数。任何多余的参数将会被忽略,正如在第三个例子中的第一个和第二个参数一样。

The arguments that bind takes are copied and held internally by the returned function object. For example, in the following code:

bind生成的函数对象在内部保持了一份参数的拷贝。例如下面的代码:

int i = 5; bind(f, i, _1); 

a copy of the value of i is stored into the function object. boost::ref and boost::cref can be used to make the function object store a reference to an object, rather than a copy:

返回的函数对象中保存的是i副本

An example for this:

#include  < iostream >
#include 
< algorithm >
#include 
< boost / bind.hpp >

using   namespace  std;

void  print_sum( int  a,  int  b)
{
    cout 
<<  (a  +  b)  <<  endl;
}

int  main()
{
    
int  arr[ 4 =  {  11 22 33 44  };

    for_each(arr, arr 
+   4 , boost::bind(print_sum, _1,  100 ));
}

result:

111
122
133
144


Boost:测试BOOST_BIND_NO_PLACEHOLDERS的程序 Boost:测试BOOST_BIND_NO_PLACEHOLDERS的程序实现功能C++实现代码 实现功能 测试BOOST_BIND_NO_PLACEHOLDERS的程序 C++实现代码 #define BOOST_BIND_NO_PLACEHOLDERS #include <boost/bind.hpp> #include <boost/core/lightweight_test.hpp> long f_0() { return 17041L; } long f_1(lo 阅读详情

相关推荐

Boost::bind使用详解

1.Boost::bind   在STL中,我们经常需要使用bind1st,bind2st函数绑定器和fun_ptr,mem_fun等函数适配器,这些函数绑定器和函数适配器使用起来比较麻烦,需要根据是全局函数还是类的成员函数,是一个参数还是多个参数等做出不同的选择,而且有些情况使用STL提供的不能满足要求,所以如果可以我们最好使用boost提供的bind,它提供了统一的接口,提供了更多的支持,比...

weixin_30501857的博客 644

boost::bind的使用方法

1.boost::bind 在STL中,我们经常需要使用bind1st,bind2st函数绑定器和fun_ptr,mem_fun等函数适配器,这些函数绑定器和函数适配器使用起来比较麻烦,需要根据是全局函数还是类的成员函数,是一个参数还是多个参数等做出不同的选择,而且有些情况使用STL提供的不能满足要求,所以如果可以我们最好使用boost提供的bind,它提供了统一的接口,提供了更多的支持,比如说...

Felaim的博客 3848

boost::bind的使用

<br />最近在几经波折之后,终于对于boost::bind有点理解了。对于习惯了其他语言的人来说,boost::bind是个挺神奇的东西,它可以将你的方法适配成任何其他的方法。其实这得益于c++的模板以及操作符重载,去看boost::bind的实现就会发现它是一个有n多重载的函数,这些重载主要是为了适应函数的参数个数。<br /> <br />其实boost::bind的原理是函数对象,而函数对象就是一个重载了()操作符的对象,这样我们就可以像调用一个方法一样来调用一个类上的这个操作符,比如a(),其实

无心云 3万+

C++ boost::bind 函数参数对数组、指针的支持不够好?欢迎指教

我们先看看下面一段代码:直接使用数组,即StreamConfigTable数组作为bind函数参数,PersistentStreamSettingsToDevice函数的第一个参数为数组形式StreamSetting_t StreamConfigTable[]。大家知道,数组名作为函数参数时候,实际退化为一个指针。 ReturnStatus CServiceEngine::SaveStreamC

铁匠Smith先生的专栏 2058

boost bind初步探究

最近对boostbind部分比较感兴趣,对其背后的机制进行了简单的分析,和大家分享一下。 注,我所看的代码是boost_1_51_0, 想来各个版本的差异不大。 从一个例子开始 直接上代码(从官方网站摘取) 定义函数 int f(int a, int b) { return a + b; } int g(int a, int b, int c) {

doon的专栏 5481

boost::bind 详解

原文:https://www.cnblogs.com/benxintuzi/p/4862129.html 使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式。其可以支持函数对象、函数、函数指针、成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数传入任意位置。 1. 通过functions和function pointers使用bi...

holyjw的博客 5690

Boost警告:关于 boost::bind 的使用

qq_45762996的博客 861

boost::asio序列: boost::bind 和 std::bind

1. 采用boost::bind #include <iostream> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time.hpp> void print(const boost::system:...

hit1524468的专栏 1399

boostbind

用于构造函数对象,其定义在文件bind.hpp中。

多看多听多总结 476

STL中的bindboost中的bind

STL中的bindboost中的bind C++11 中的std::function和std::bind boost::bind 函数绑定器

qq_40028201的博客 926

boostbind源码剖析

1.为什么使用bind引用《Beyond the C++ Standard Libarary: An introduction to Boost》中的原文,如下:a. 使函数和函数对象适用于标准库算法b. 使用一致的语汇法创建绑定器binderc. 强大的函数组合(functional composition)2. 使用bind的例子,请参考《Beyond the C++ Standard Lib

overcomeunicom的专栏 2948

Please use boost/bind/bind.hpp + using namespace boost::placeholders

The practice of declaring the Bind placeholders (_1, _2, …) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior. 提示war

Betterc5的博客 4389

BOOST::bind 如何使用

boost::bind 是标准函数std::bind1st和std::bind2nd的泛型。它支持函数、函数对象、函数指针和成员函数指针。它可以绑定任何参数到一个具体的值或者函数到预定义好的位置。-----------------在函数和函数指针中使用-----------------int f(int a, int b){  return a + b;}

liuchangyu23的专栏 3896

boost bind使用指南

bind - boost头文件: boost/bind.hppbind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不下去. 这里只记下它的用法: 9.1 对于普通函数假如有函数 fun() 如下:  void fun(int x, int y) {  cout  }现在我们看看怎么用bind 向其绑

Make Progress Everyday! 4万+

C++ boost bindboost function的使用

boost bind/function库的出现,替代了stl中的mem_fun,ptr_fun,bind1st,bin2nd等函数  //用于函数适配器 //使用示例#include #include #include using namespace std; class Foo { public: void memberFunc(double d, int i, int j) { /

晴天的专栏 1298

boostbind使用感受

boost库的bind真的很灵活,与STL结合的也很紧密,省去了不少的麻烦,还可以嵌套使用,但是嵌套的多了就会阅读困难了,所以,个人感觉跟boost::function结合使用应该是个不错的选择。  #include #include #include #include void StaticFunc(){ std::cout<<"Hello! This i

百变萌菌 2440

boost::functionboost::bind

http://www.xumenger.com/cpp-boost-bind-function-20180612/ boost::function boost::function是一个函数包装器,也即一个函数模板,可以用来代替拥有相同返回类型,相同参数类型,以及相同参数个数的各个不同的函数 #include&lt;boost/function.hpp&gt; #include&lt;ios...

zzhongcy的专栏 5026

boost::bind 方法浅析

1.使用方法: boost bind 方法一般需要和boost function配合使用,因为用boost bind 之后返回的会是一个boost functionboost functionboost::function f; //声明了一个无返回值,参数为int的boost function。 1.绑定普通函数 void fun(int a,int b); f = boos

风随雨去 1145
上一篇: 经典收藏 C++内存管理操作详解
下一篇: const 和 readonly 的区别
jsjwql
博客等级 码龄24年 33粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值