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. gcc 中weak弱函数

    1.weak弱函数 weak 函数用于定义变量或者函数.弱函数一般用于多个模块间的交互接口 int __attribute__((weak)) test_lib_a(int a, int b) { p ...

  2. clickHouse安装踩坑

    1.查看版本  clickhouse-server --version 2.下载 linux机器上 wget下载,有两个安装包体积较大 wget https://packages.clickhouse ...

  3. gorm去重查询 iris框架

    写练习 demo 时遇到需要进行去重查询,gorm没有db.distinct()的写法 // 数据库的表字段 type Pro_location_relation struct { Id int64 ...

  4. windows下rabbitmq启动报错--distribution port 25672 in use by another node: rabbit@DESKTOP-LLPGVVE

    最近公司有需求需要用到rabbitmq,因为之前习惯用的都是activemq,所以要临时学习一下,捣鼓这个rabbitmq.想着先在本地捣鼓测试一下,然后按照这个博主分享的安装方式进行安装. http ...

  5. Cloud9 3.0 SDK安装

    Cloud9 IDE是一个基于Node.JS构建的JavaScript程序开发Web IDE.它拥有一个非常快的文本编辑器支持为JS, HTML, CSS和这几种的混合代码进行着色显示.Cloud9 ...

  6. spring boot整合druid

    其实网上有很多例子可供参考,主要是在整合的过程中遇到了一些问题,方便记录下.另外例子可参考以下两个链接: https://www.jianshu.com/p/e3cd2e1c2b0c https:// ...

  7. Android中保存文件到内部存储器

    1 public static void saveDataToPrivateFile(Context context, String data, int mode, String fileName) ...

  8. 杭电oj 蟠桃记

    Problem Description 喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!什么问题?他研究的问题是蟠桃一共有多少 ...

  9. tomcat的安装以及环境配置

    1.Tomcat的下载地址:http://tomcat.apache.org/ Tomcat是开放源代码的WEB服务器,安装时,只需解压压缩包即可 2.环境变量的配置 1>新建系统变量CATAL ...

  10. memoのQt自动调整窗口尺寸

    折腾了好久,好久.终于搞出一个自认为还算可以的方案: QTimer::singleShot(0, this, [this]{ this->adjustSize(); }); 这个解决方案确实有点 ...