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. postgressql知识点、源码

    1.新增语法,tab键自动联想的代码位置. tab-complete.c中的函数pgsql_completion

  2. delphi get post

    procedure GetDemo;var IdHttp : TIdHTTP; Url : string;//请求地址 ResponseStream : TStringStream; //返回信息 R ...

  3. 最小化安装debian10&gnome最小化安装

    直到后面配置网络源之前都是断网安装,因为debian security好像总是要去总源找点东西,所以即便你选择国内源甚至不选择网络源安装,依然会莫名 的失败. I. 最小化安装debian10(用ro ...

  4. docker之安装tomcat

    国内Image仓库地址:https://hub.docker.com/search?q=tomcat 安装tomcat docker pull tomcat 查看Image docker images ...

  5. 关于import-route static 和default-route-advertise区别知识总结

    关于import-route static 和default-route-advertise区别知识总结 一.相关解释 import-route static  命令不能引入外部路由的默认路由,OSP ...

  6. PyTables学习 (数据保存形式,对象树结构)

    参考自http://www.pytables.org/usersguide/introduction.html PyTables的主要目的是提供一个好的操作HDF5文件的方法. HDF文件是分层数据格 ...

  7. Android拍照程序适配

    public void takePic(){ String forderPath = getExternalFilesDir("") + "/pic"; Fil ...

  8. linux源码-概览

    BootloaderWin    Bios Linux UbootAndroid fastboot linux源码宏观结构boot:启动linux时要用到的引导代码 bootsect.s 磁盘引导文件 ...

  9. 记录自己在对订单进行按日期查询时使用的一种查询的方法,这里的orders是订单表,你也可以改成别的什么表对于最终数据不会造成影响,除非你那个表的数据只有几条那样就会出现查不到日期的情况

    SELECT @date := DATE_ADD(@date, INTERVAL + 1 DAY) days FROM ( SELECT @date := DATE_ADD("2019-06 ...

  10. springboot集成es7(基于high level client)

    环境: ES: 7.12.0 1.springboot工程引入es相关jar <dependency> <groupId>org.elasticsearch</group ...