bind-函数调用包装器-详解
来源:互联网 发布:甩脂机能减肥吗 知乎 编辑:程序博客网 时间:2024/05/08 04:18
在头文件functional
中定义,函数模板生成函数调用包装器。
template< class F, class... Args >/*unspecified*/ bind( F&& f, Args&&... args ); (1) (since C++11)template< class R, class F, class... Args >/*unspecified*/ bind( F&& f, Args&&... args ); (2) (since C++11)
调用这个包装器就是在调用这个包装起绑定的函数,使用的参数就是参数列表中的参数。
#include <random>#include <iostream>#include <memory>#include <functional>void f(int n1, int n2, int n3, const int& n4, int n5){ std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';}int g(int n1){ return n1;}struct Foo { void print_sum(int n1, int n2) { std::cout << n1+n2 << '\n'; } int data = 10;};int main(){ using namespace std::placeholders; // for _1, _2, _3... // demonstrates argument reordering and pass-by-reference int n = 7; // (_1 and _2 are from std::placeholders, and represent future // arguments that will be passed to f1) // _2的意思就是新函数 f1 的第二个参数绑定到原函数 f 的第一个参数位置 // 第四个参数传递的是引用,第五个参数传递的是值 auto f1 = std::bind(f, _2, _1, 42, std::cref(n), n); n = 10; f1(1, 2, 1001); // 1 is bound by _2, 2 is bound by _1, 1001 is unused // 结果是 2 1 42 10 7 其中 1001 是无效的 // nested bind subexpressions share the placeholders auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5); f2(10, 11, 12); // 嵌套绑定,f 的第四个和第五个参数值绑定为4和5. // f 的第一个和第三个参数绑定为 f2 的第三个参数,为12 // f 的第二个参数绑定为 g 函数的返回值,g 的参数是 f2 的第三个参数 // 也就是上面说的 share the placeholders // common use case: binding a RNG with a distribution std::default_random_engine e; std::uniform_int_distribution<> d(0, 10); std::function<int()> rnd = std::bind(d, e); // a copy of e is stored in rnd // 绑定到函数 d 上,参数是 e ;rnd 是一个函数对象,返回值是int for(int n=0; n<10; ++n) std::cout << rnd() << ' '; std::cout << '\n'; // bind to a member function Foo foo; auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1); // 绑定到成员函数上 // 函数是 print_sum ,对象实例是 foo , 第一个参数是95 ,第二个参数是 f3 的第一个参数 f3(5); // bind to member data auto f4 = std::bind(&Foo::data, _1); std::cout << f4(foo) << '\n'; // smart pointers can be used to call members of the referenced objects, too std::cout << f4(std::make_shared<Foo>(foo)) << '\n' << f4(std::unique_ptr<Foo>(new Foo(foo))) << '\n';}
0 0
- bind-函数调用包装器-详解
- C++ Boost::bind函数包装器使用,boost::bind与伪函数的绑定使用
- bind函数详解
- bind函数详解
- java包装器详解
- C++函数包装器
- 函数包装器
- 函数对象,lamdda表达式,function函数包装,std::bind函数绑定
- Boost库bind函数的嵌套调用
- jQuery bind 绑定事件函数详解
- TCP/IP编程之bind函数详解
- boost库之bind函数详解
- 类或函数包装器
- bind()适配器(Adapter) -- 调用全局函数及成员函数
- 函数包装器,函数私有类处理
- c++:函数模板与函数包装器
- 系统调用处理 暨 错误的包装函数
- bind()详解
- Win8 Metro(C#)数字图像处理--2.52图像K均值聚类
- bmp图像读取
- Cordys BOP 4平台开发入门实战演练——会签流程建模开发
- lua与C/C++交互table(lua_gettble)
- 使用 JavaScript 实现 iframe 判断和页面重定向
- bind-函数调用包装器-详解
- 欢迎使用CSDN-markdown编辑器
- iOS UIButton 设置图片文字垂直排列
- 五步git操作搞定Github中fork的项目与原作者同步
- Android 自定义Drawable 资源引用问题
- socket通信框架mina使用详解
- C语言gets雨scanf函数的用法
- Android开发学习笔记之service
- Boost Asio库的学习与探究(一)