std::ref
The std::thread
constructor copies the supplied values, without converting to the expected argument type (which is reference type in this case, seeupdate()
). So we need to wrap the arguments that really needs to be references in std::ref
.
So, in std::thread the std::ref in to make in compile with correct augument type.
Also, an example where std::ref is useful
std::vector<int> v1, v2; void test() {
for (std::vector<int>& vv :
// Compiles
{ std::ref(v1), std::ref(v2) } // Compiler rejects this with:
// binding reference of type 'vector<...>' to value of
// type 'const vector<...>' drops 'const' qualifier
// { v1, v2}
) {
vv.push_back(3);
}
std::ref的更多相关文章
- 为什么C++11引入了std::ref?
C++本身有引用(&),为什么C++11又引入了std::ref? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: #include <f ...
- C++11 std::ref使用场景
C++本身有引用(&),为什么C++11又引入了std::ref(或者std::cref)? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: # ...
- c++11之为什么C++11引入了std::ref?
C++本身有引用(&),为什么C++11又引入了std::ref? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: #include <f ...
- std::ref() 与 &
引言 之前因为调整样式把博客园的样式毁了,所以一直在自己的另一个博客上更新,有兴趣的可以去观望一下:http://blog.yunlambert.top/最近还是把博客园拾起来吧..... 最近看到一 ...
- c++11多线程---std::ref和std::cref
std::ref和std::cref 解释 std::ref 用于包装按引用传递的值. std::cref 用于包装按const引用传递的值. 为什么需要std::ref和std::cref ...
- std::ref和std::cref使用(转载)
转载于:https://blog.csdn.net/lmb1612977696/article/details/81543802 std::ref和std::cref 解释: std::ref 用于包 ...
- std::bind与std::ref, why and how
首先解释下为什么有时候需要bind. 我们可以用bind从函数T add(T a, T b)造出个inc()来,即把b写死为1.这个例子本身比较傻,但有不傻的应用. template<typen ...
- boost和std中的thread的引用参数
boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...
- 用C++11的std::async代替线程的创建
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
- std::thread
std::shared_ptr<std::thread> m_spThread; m_spThread.reset(new std::thread(std::bind(&GameS ...
随机推荐
- input放入焦点,选中全部文本
async mounted(){ let inputList = document.querySelectorAll('input'); for (let index = 0; index < ...
- 第三周day4
第三周day4,星期四 所用时间:1h 代码量:0 博客量:2 了解到的知识点:Toast.
- 本地搭建JupyterNotebook开发环境
背景 Jupyter 是一款优秀的编程语言运行环境包括Hub.Lab.Notebook等优秀自项目,JupyterNotebook是衍生的在线交互运行平台的前端项目 环境 Windows 11 Nod ...
- 浅谈Redis大Key与热Key
如何定义大 Key 和 热 Key 如何定义大 Key 如何定义热 Key 大 Key 和 热 Key 产生的原因 大 Key 和 热 Key 有哪些危害 大 Key 的危害 热 Key 的危害 如何 ...
- zookeeper(1)-集群的搭建
集群搭建 1. 下载二进制文件 $ wget --no-check-certificate https://mirrors.ustc.edu.cn/apache/zookeeper/zookeeper ...
- vue使用websoket
参考链接:https://www.cnblogs.com/qisi007/p/10213886.html export default { name: "realdetail", ...
- 回溯-1-N皇后(Backtracking-1-N Queens)
#include <stdio.h> #define N 4 enum bool {TRUE, FALSE}; void print_Q(int *Q) { int i; for (i = ...
- PowerShell学习笔记五_模块
PowserShell其他技巧 1. 执行完一个ps1文件后,不回收 有一个场景,在fun.ps1中,仅仅写了一段 Funtion fun([String] input) { } 然后打开PowerS ...
- 在Vue中实现app拍照-选取本地图库-图片上传成功后预览
基于Vue和uni-app实现手机app的功能实现和打包.拍照功能和选取本地图片使用的是HTML5的API 实现. 我为测试这个功能使用node写了个本地服务器,对于手机调试,可以通过连接同一个无线网 ...
- fftw安装
1. 下载fftw 2.tar -zxvf fftw.tar.gz 3. ./configure --prefix=path --enable-sse2 --enable-avx --enable-f ...