c++11: bind用法
原型:
template< class R, class F, class... Args >
bind( F&& f, Args&&... args );
bind函数模板的作用是:
The function template bind generates a forwarding call wrapper for f. Calling this wrapper is equivalent to invoking f with some of its arguments bound to args.
不怎么好翻译,尝试解释一下:我们为某个函数做一个bind,然后调用该bind和调用函数是一样的,跟函数指针有点像。
#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 = ;
}; int main()
{
using namespace std::placeholders; // for _1, _2, _3... // demonstrates argument reordering and pass-by-reference
int n = ;
// (_1 and _2 are from std::placeholders, and represent future
// arguments that will be passed to f1)
auto f1 = std::bind(f, _2, _1, , std::cref(n), n);
n = ;
f1(, , ); // 1 is bound by _1, 2 is bound by _2, 1001 is unused // nested bind subexpressions share the placeholders
auto f2 = std::bind(f, _3, std::bind(g, _3), _3, , );
f2(, , ); // bind to a member function
Foo foo;
auto f3 = std::bind(&Foo::print_sum, &foo, , _1);
f3(); // bind to member data
auto f4 = std::bind(&Foo::data, _1);
std::cout << f4(foo) << '\n';
c++11: bind用法的更多相关文章
- C++11 bind和function用法
function是一个template,定义于头文件functional中.通过function<int(int, int)> 声明一个function类型,它是“接受两个int参数.返回 ...
- C/C++ C++ 11 std::function和std::bind用法
std::bind() std::bind 主要用于绑定生成目标函数,一般用于生成的回调函数,cocos的回退函数都是通过std::bind和std::function实现的.两个点要明白: 1.绑定 ...
- 【转】javascript笔记之apply、call、bind用法
原文地址:https://www.cnblogs.com/coco1s/p/4833199.html apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数 ...
- JS中的call、apply、bind 用法解疑
JS中的caller arguments.callee call apply bind方法 一.call()和apply()方法 1.方法定义 call方法: 语法:call([thisObj ...
- c++ 11 bind function
Year 2011陈 良乔C++11 FAQ std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理 ...
- golang(11) 反射用法详解
原文链接:http://www.limerence2017.com/2019/10/14/golang16/ 反射是什么 反射其实就是通过变量动态获取其值和类型的一种技术,有些语言是支持反射的比如py ...
- C++11 新用法
基于哈希的 map 和 set 简述 基于哈希的 map 和 set ,它们分别叫做 unordered_map, unordered_set .数据分布越平均,性能相较 map 和 set 来说提升 ...
- [已解决] MyBatis 中bind用法
JAVA: TC_ENTR_FLOW selectFlowForUpdate(String ENTR_ID); XML: <select id="selectFlowForUpdate ...
- php中Closure::bind用法(手册记录)
手册中 Closure::bind — 复制一个闭包,绑定指定的$this对象和类作用域. 具体参数可以看手册,这里记录下这个方法的实际用处. <?php trait MetaTrait { p ...
随机推荐
- linux中mail函数不能发送邮件怎么办
原因分析: 1.检查下本机25端口是否有开放 2.mail是利用linux自带的sendmail服务来发送邮件的,service sendmail status 查看下该服务的状态 发送命令:mail ...
- mkinitrd---简单介绍
转载:http://blog.csdn.net/zwcq82/article/details/4295481 原来对mkinitrd不是很了解.最近做内核升级,需要制作信息的initrd文件,发现出错 ...
- Joy of Programming: Understanding Bit-fields in C
转:http://www.linuxforu.com/2012/01/joy-of-programming-understanding-bit-fields-c/ By S.G. Ganesh on ...
- 3.1html学习之列表
一.含义: ul:unorder list ol:order list li:list item dl:definition list dt:definition term dd:definition ...
- C#采用Winform实现类似Android的Listener
本文实例讲述了C#采用Winform实现类似Android下Listener的方法.非常实用的一个技巧.具体实现方法如下: 首先是一个ICallBackListener接口,里面要写上你需要Liste ...
- 将远程数据库中的某表数据复制到本数据库(ORACLE)
1. 建立 DATABASE LINKCREATE PUBLIC DATABASE LINK ABCCONNECT TO SA IDENTIFIED BY PASSWORDUSING '(DESCRI ...
- Java基础知识强化105:打印数组的方法总结
1. 使用for循环打印数组. 2. 使用Arrays工具类,将数组转化为有序的List打印出来. 3. 使用Arrays工具类,使用Arrays.toString()输出数组内容. 上面三种方法打印 ...
- VMware系统运维(四)SQL Server 2008 R2 新建数据库与ODBC数据
1.新建数据库,右击"新建数据库-." 2.设置数据库名,初始数据库大小,如果要配置数据库优化,可以考虑初始大小与日志大小,点击添加安装完成,即完成数据库新建. 3.ODBC数据源 ...
- 在centos7中添加一个新用户并授权
参考地址:http://www.cnblogs.com/woshimrf/p/5906084.html 创建新用户 创建一个用户名为:zhangbiao [root@localhost ~]# add ...
- oc中的分类/协议/属性
1.分类:当我们想给某个类加一些方法时,如果不想通过继承这个类来实现,可以通过分类给这个类加一些行为,这个过程与继承相比更加轻量化. @interface NSString (SubClass) -( ...