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 ...
随机推荐
- 手机端的tab切换,响应式切换效果
之前写过这些tab切换的效果,无论网页上还是手机端,网上也有很多的例子,这个好像是我参考网上,也不知道是哪里的了.总结了一下,就当保存下来了把. <!DOCTYPE html > < ...
- C#基础--局部类型Partial
局部类型 原本来在同一个命名(namespace)空间下 是不允许相同的类(class)名存在的 但是partial关键字可以允许在同一个namespace下有想通过的类名存在 写法 下面的两个不 ...
- Intervals poj 1201 差分约束系统
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22503 Accepted: 8506 Descri ...
- 1.7.6 Highlighting-高亮
1 高亮 solr的高亮允许匹配用户查询的文档的片段包含在查询响应中返回,高亮包含在返回结果的单独部分(highlighting部分). solr提供了一个高亮工具的集合,这个工具允许控制大量字段的片 ...
- Java SE --- 自增自减
关于变量的自增与自减运算. 1) int b = a++,作用是将a的值先赋给b,然后再让a自增1. 2) int b = ++a,作用是将a的值先自增1,然后将自增后的结果赋给b. 直接上例 ...
- react-redux 学习笔记
react 是 view 层的一个框架,负责展示数据:redux 控制数据流动,把数据存在唯一的 store 里,通过 action 来触发事件,reducer 来根据事件处理数据. redux 在通 ...
- Android隐藏状态栏实现沉浸式体验
转自: Android状态栏微技巧,带你真正理解沉浸式模式 什么叫沉浸式? 根据百度百科上的定义,沉浸式就是要给用户提供完全沉浸的体验,使用户有一种置身于虚拟世界之中的感觉. 那么对应到Android ...
- 《算法导论》习题解答 Chapter 22.1-2(邻接矩阵与链表)
链表如图: 矩阵: 1 2 3 4 5 6 7 1 0 1 1 0 0 0 0 2 1 0 0 1 1 0 0 3 1 0 0 0 0 1 1 4 0 1 0 0 0 0 0 5 0 1 0 0 0 ...
- (转)内网网站发布到外网-nat123动态公网IP动态域名解析
环境描述: 路由器分配的是动态公网IP,且有路由器登录管理权限,网站服务器部署在路由器内部网络.如何将内网网站发布到外网大众访问? 解决方案: 内网使用nat123动态域名解析,将域名实时固定解析到路 ...
- apache2.4 +django1.9+python3+ubuntu15.10
这是我这几天学习部署django的总结,中间出现了不少的问题.特此记录下来,用来复习巩固,同时也希望给想学习的同学一些参考. 第一步:我在ubuntu上装的是python3.sudo apt-get ...