std::bind与std::ref, why and how
首先解释下为什么有时候需要bind. 我们可以用bind从函数T add(T a, T b)造出个inc()来,即把b写死为1。这个例子本身比较傻,但有不傻的应用。
template<typename T> T add(T a, T b) { return a + b; }
template<typename T> auto get_inc() {
T b = 1;
return bind(add<T>, std::placeholders::_1, b);
}
std::function<int(int)> inc = get_inc<int>();
printf("%d\n", inc(2));
其次为啥bind默认传copy而不是reference? get_inc()返回后, 它里面的b就没了,stack frame供别的函数用去了。传引用的话就引错地方了。
可有时候很需要传应用,如两个vector相加,而且我们知道引用是有效的:
#include <stdio.h>
#include <vector>
#include <functional>
using namespace std; struct vec : public vector<int> {
vec() {}
vec(const vec&) { puts("vec(const vec&)"); }
vec& operator=(const vec&) { puts("operator=(const vec&)"); return *this; }
}; void addv(const vec& a, const vec& b) {} // 没有返回值不重要 int main() {
std::function<int(int)> inc = get_inc<int>();
printf("%d\n", inc(2)); vec a, one;
auto incv = bind(addv, std::placeholders::_1, ref(one));
incv(a);
}
如果把ref(one)换成one,可看到copy constructor被调用。可以在某个函数里new个vec* v, 再ref(*v),最后某处delete v.
std::ref是如何实现的?Function templates ref and cref are helper functions that generate an object of type std::reference_wrapper, using template argument deduction to determine the template argument of the result. [cppreference]
How does c++11 std::ref work? - Stack Overflow 我搞了个指针版:
template<class T> struct myref_wrapper {
T* p;
myref_wrapper(T& t) : p(&t) {}
T& operator()() { return *p; }
};
template<class T> myref_wrapper<T> myref(T& t) { return myref_wrapper<T>(t); }
int i = 0;
myref(i)() = 1;
printf("%d\n", i);
真是人生苦短,我学python啊。c++ - Implementation of std::reference_wrapper std::bind简单使用 - 博客园 Passing reference with std::ref in C++
std::bind与std::ref, why and how的更多相关文章
- std::bind和std::function
std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数,特别是在多线程的程序中,经常用它将函数进行包装,然后打包发送给工作线程,让工作线程去执行我们的任务. ...
- c++11特性与cocos2d-x 3.0之std::bind与std::function
昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0 ...
- 第12课 std::bind和std::function(3)_std::function可调用对象包装器
1. std::function (1)首先是一个类模板,用于包装可调用对象.可以容纳除了类成员(函数)指针之外的所有可调用对象. (2)可以将普通函数,lambda表达式和函数对象类统一起来.尽管它 ...
- 第11课 std::bind和std::function(2)_std::bind绑定器
1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. ( ...
- 第10课 std::bind和std::function(1)_可调用对象
1. 几种可调用对象(Callable Objects) (1)普通函数指针或类成员的函数指针 (2)具有operator()成员函数的类对象(仿函数).如c++11中的std::function类模 ...
- c++11之std::bind简单使用
note 更多用法,请参考: cppreference 用的少,容易忘. 我的理解 类似延迟计算. 比如,回调函数,将回调函数传入后,回调函数不一定马上被调用. 它是一个模板类,调用后将生成一个新的调 ...
- C++11 std::bind std::function 高级使用方法
从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...
- C++ 11 笔记 (四) : std::bind
std::bind 接受一个可调用的对象,一般就是函数呗.. 还是先上代码: void func(int x, int y, int z) { std::cout << "hel ...
- std::bind 的使用说明
转自: https://www.cnblogs.com/cmranger/p/4743926.html ///////////////////// std::bind bind是对C++98标准中函数 ...
随机推荐
- MEGAN4,MEGAN5和MEGAN6的Linux安装和使用
目录 MEGAN 4 MEGAN 5 MEGAN 6 MEGAN(Metagenome Analyzer)是宏基因组学进行物种和功能研究的常用软件,实际上现在的Diamond+MEGAN6已经是一套比 ...
- PAML 选择压力的计算
简介 PAML(Phylogenetic Analysis by Maximum Likelihood)是伦敦大学的杨子恒(Yang Ziheng)教 授开发的一套基于最大似然估计来对蛋白质和核酸序列 ...
- AnnotationHub, clusterProfiler 进行GO,KEGG注释
️ AnnotationHub 目前最新的工具包叫做AnnotationHub,顾名思义,就是注释信息的中装站.通过它,能找到了几乎所有的注释资源.如果没有,你还可以根据已有的数据用它提供的函数进行构 ...
- 使用BRAKER2进行基因组注释
来自:https://www.jianshu.com/p/e6a5e1f85dda 使用BRAKER2进行基因组注释 BRAKER2是一个基因组注释流程,能够组合GeneMark,AUGUSTUS和转 ...
- Golang: map类型切片内存分配
切片ik通过索引访问,然后为每个map分配内存: 切片jk通过获得切片内每个元素的拷贝来分配内存,并未成功为切片内每个map分配内存,使用时赋值也就失败了 1 package main 2 3 imp ...
- python—模拟生成双色球号
双色球规则:"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中不重复选择:蓝色球号码从1--16中选择. # -*- coding:UTF-8 - ...
- pyyaml模块
pyyaml模块是一种文件数据处理格式的方法,常用与生成.解析或修改.yaml配置文件 1.常见.yaml文件格式内容如下 languages: - Ruby - Perl - Python webs ...
- Mac下source tree 下的安装
安装时出现了以下错误,解决方法 git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=source ...
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享四:部署到阿里云
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 阿里云开放必要端口,mysql与t ...
- college-ruled notebook
TBBT.s3.e10: Sheldon: Where's your notebook?Penny: Um, I don't have one.Sheldon: How are you going t ...