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. vscode中使用powershell显示分支名

    https://blog.csdn.net/weixin_43932597/article/details/125000557 windows powershell(或windows terminal ...

  2. vue webpack打包之后 重新修改配置文件接口API路径,无需修改代码后再打包

    用vue-cli构建的项目通常是采用前后端分离的开发模式,也就是前端与后台完全分离,此时就需要将后台接口地址打包进项目中,但是有的时候需要修改接口地址,为了避免为了修改接口地址而进行修改代码后再重新打 ...

  3. 学习-Vue2-Vue实例-数据与方法-数据的响应式

    当一个实例被创建时,它将data对象中的所有的property加入到Vue的响应式系统中. 当这些property的值发生改变时,视图将会产生"响应",即匹配更新为新的值.当这些数 ...

  4. restful的10个规范、序列化和反序列化的名词解释

    # 概念 REST全称是Representational State Transfer,中文意思是表述:表征性状态转移. RESTful是一种定义Web API接口的设计风格,尤其适用于前后端分离的应 ...

  5. 微信小程序监听view到顶部的高度

    view style='width:100%;height:80rpx;' id='navigation'></view> wx.createSelectorQuery().sele ...

  6. WINDOWS 下 Visual Studio Code + Odoo12 错误解决方法

    1. error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio&quo ...

  7. response status is 500 https://localhost:7129/swagger/v1/swagger.json

    SwaggerGeneratorException: Conflicting method/path combination "GET Test" for actions - To ...

  8. What is REST and Restful?

    什么是rest 和 restful? 提出rest的作者,目的:符合框架原理的情况下,理解和评估以网络为基础的应用软件的架构设计,得到一个功能强,性能好,适宜通讯的架构. Fielding将他对互联网 ...

  9. 【C++复习】第八章 多态性(1)(多态类型、运算符重载)

    1.多态性 1.1 什么是多态? 多态是指相同消息被不同对象接收后导致不同的行为,所谓消息是指对类成员函数的调用,不同的行为是指不同的实现,也就是调用了不同的函数. 消息在C++编程中指的是对类的成员 ...

  10. 函数调用_通过apply和call方法调用

    不同类型函数调用之间的主要区别在于:最终作为函数上下文(可以通过this参数隐式引用到)传递给执行函数对象不同.对于方法而言,即为所在的对象:对于函数而言是window或是undefined(取决于是 ...