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 ...
随机推荐
- LeetCode30 Substring with Concatenation of All Words
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- 测试你是否和LTC水平一样高
Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z^2= num的一个正整数解. ...
- 【Android 界面效果13】关于全屏和取消标题栏
------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 去掉标题栏: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Wi ...
- apply和call的区别在哪里
apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...
- CentOS对新加入的硬盘格式化
[root@rac1 ~]# fdisk /dev/sdbDevice contains neither a valid DOS partition table, nor Sun, SGI or OS ...
- 转:从三层架构到MVC-MVP
当然这种架构模式本身的一些问题也会在接下来的内容就加以介绍,另外就是如果大家有什么不同观点的话,欢迎拍砖(只要不打脸就行,呵呵). 一. MVC是谁提出的 模型-视图-控制器(MVC)是Xerox P ...
- Linux之装机指南
软件: Variety:桌面壁纸自动切换 sudo add-apt-repository ppa:peterlevi/ppa sudo apt-get update sudo apt-get inst ...
- 【转】PS学堂之一:展示一下自己做的圆形印章
共分七个步骤: 1.点击文件--新建,新建一个500×500像素,背景为透明的文件,选择RGB颜色. 2.把前景色和文字颜色设置为正红(R为255,G和B为0). 3.在视图下拉菜单中选择标尺,将横. ...
- 文件上传利器SWFUpload入门简易教程
凡做过网站开发的都应该知道表单file的确鸡肋. Ajax解决了不刷新页面提交表单,但是却没有解决文件上传不刷新页面,当然也有其它技术让不刷新页面而提交文件,该技术主要是利用隐藏的iFrame, 较A ...
- AngularJS 学习笔记(1)
AngularJS是一款前端JS框架.AngularJS官网 http://angularjs.org [开发环境准备]: 1,下载AngularJS:JS and CSS in Solution 2 ...