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的更多相关文章

  1. 为什么C++11引入了std::ref?

    C++本身有引用(&),为什么C++11又引入了std::ref? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: #include <f ...

  2. C++11 std::ref使用场景

    C++本身有引用(&),为什么C++11又引入了std::ref(或者std::cref)? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: # ...

  3. c++11之为什么C++11引入了std::ref?

    C++本身有引用(&),为什么C++11又引入了std::ref? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: #include <f ...

  4. std::ref() 与 &

    引言 之前因为调整样式把博客园的样式毁了,所以一直在自己的另一个博客上更新,有兴趣的可以去观望一下:http://blog.yunlambert.top/最近还是把博客园拾起来吧..... 最近看到一 ...

  5. c++11多线程---std::ref和std::cref

    std::ref和std::cref   解释 std::ref 用于包装按引用传递的值. std::cref 用于包装按const引用传递的值.   为什么需要std::ref和std::cref ...

  6. std::ref和std::cref使用(转载)

    转载于:https://blog.csdn.net/lmb1612977696/article/details/81543802 std::ref和std::cref 解释: std::ref 用于包 ...

  7. std::bind与std::ref, why and how

    首先解释下为什么有时候需要bind. 我们可以用bind从函数T add(T a, T b)造出个inc()来,即把b写死为1.这个例子本身比较傻,但有不傻的应用. template<typen ...

  8. boost和std中的thread的引用参数

    boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...

  9. 用C++11的std::async代替线程的创建

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

  10. std::thread

    std::shared_ptr<std::thread> m_spThread; m_spThread.reset(new std::thread(std::bind(&GameS ...

随机推荐

  1. 3516A调试

    最近在调Hi3516A的板,硬件不知道为什么如此设计,用一片16bit4G的ddr,16Mspi flash,按理如果是A应该是2片16bit的ddr,组成32位总线,现在怕是只能当D来用了,编译成功 ...

  2. how to make the windows console works with utf-8 encoded project

    the console of the windows os is not working in the utf-8 encoding, by default. When you force your ...

  3. shell—if + case条件语句

    if 条件语句 1. 概述 在shell的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结果执行不同的操作,有时候也会与 if 等条件语句相结合,来完成测试判断,以减少程序运行错误. 2. ...

  4. Docker 数据迁移到数据盘

    systemctl stop docker 找到新的.空间较达的磁盘路径,然后创建任意目录.例如: mkdir /data/docker mv /var/lib/docker /data/docker ...

  5. Vulnhub 靶机 CONTAINME: 1

    Vulnhub 靶机 CONTAINME: 1 前期准备: 靶机地址:https://www.vulnhub.com/entry/containme-1,729/ kali地址:192.168.147 ...

  6. 【godis】skiplist

    skiplist 前言:在看代码时看到 ZSKIPLIST_MAXLEVEL = 32,当时并不了解 ZSKIPLIST_P 的作用,想着用 2 分法不应该层数是 64 吗?书上和他人的代码都是基于 ...

  7. pgsql 自定义函数

    CREATE OR REPLACE FUNCTION test1(id INTEGER,id1 INTEGER) RETURNS INTEGER LANGUAGE plpgsql AS $$ decl ...

  8. go标准库之fmt

    go标准库之fmt fmt库 Print系列 1. Print 不换行 2. Println 换行 3. Printf 不换行,可以使用格式化占位符 格式化占位符 占位符 说明 通用 --- %v 值 ...

  9. C语言-链表流星雨(EsayX)

    刷B站看到的,做个玩玩.IDE:Visual Studio 2022.依赖EsayX图形库 1-效果 2-程序 /* 链表流星雨单文件版本 依赖EsayX图形库 */ #include <std ...

  10. Linux下mysql安装教程

    一 环境准备 1.检查是否已经安装过mysql,执行命令 [root@localhost /]# rpm -qa | grep mysql 从执行结果,可以看出我们已经安装了mysql-libs-5. ...