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 ...
随机推荐
- 其他计算机&网络&行业知识
互联网数据中心(IDC) VIDC(端口映射) CVM云服务器(Cloud Virtual Machine) IDE 集成开发环境: 开发工具 QA:Quality Assurance,直译为质 ...
- uniapp 扫描
借鉴链接:https://blog.csdn.net/qq_33165549/article/details/89879435 1.扫描页面 <template> <view> ...
- RTC@@@Real-Time Clock(实时时钟的简称)及电路问题分析
RTC@@@Real-Time Clock(实时时钟的简称) 实时时钟(Real-Time Clock)是PC主板上的晶振及相关电路组成的时钟电路的生成脉冲,提供稳定的时钟信号给后续电路用.主要功能有 ...
- 一、mysql基础
说明:学习视频参考尚硅谷--康师傅 第一章.数据库概述 1.为什么使用数据库?why? 持久化(persistence):把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数 ...
- ubuntu-wireshark打开出现错误的问题
The capture session could not be initiated on interface 'enp2s0' (You don't have permission to captu ...
- 一个MySQL双引号把我坑惨了!
一.前言 最近经常碰到开发误删除误更新数据,这不,他们又给我找了个麻烦,我们来看下整个过程,把我坑得够惨. 二.过程 由于开发需要在生产环节中修复数据,需要执行120条SQL语句,需要将数据进行更新, ...
- sdp安装及实例
环境: sdpserver:192.168.1.160 sdpclient:192.168.1.161 安装 yum install gcc gcc-c++ libpcap* libtool* wge ...
- 【翻译】了解Flink-对DataStream API的介绍 -- Learn Flink-Intro to the DataStream API
目录 流式可以传输什么? Java元组和POJO 元组 POJO Scala元组和case classes 一个完整的例子 流执行环境 基本的stream sources 基本的stream sink ...
- unity笔记001
熟悉场景和物体的基本操作,q移动场景,e旋转物体,放大缩小,V键顶点吸附,相比快捷键,还是喜欢用xyz参数控制
- pat乙级1012数字分类
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int ...