c++11 std::ref std::cref

参考:

C++已经有了引用操作符&为什么C++11还要引入std:ref

std::ref和std::cref使用

&是类型说明符,而std::ref是一个函数,返回std::reference_wrapper(类似于指针)

为什么需要std::ref?(std::cref类似)

主要是考虑到c++11中的函数式编程,例如:std::bind

std::bind在使用时,是对参数直接拷贝,而不是引用

发现这个问题的契机是在使用thread的标准库时

#include<iostream>
#include<thread>
#include<string>

using namespace std;

void foo( int &a)
{
    cout<<"thread :"<< a++ <<endl;
}

int main()
{
    int num = 0;
    thread t1(foo, std::ref(num));
    thread t2(foo, std::ref(num));
    t1.join();
    t2.join();
    return 0;
}

默认是按值传递,需要通过std::ref按引用传递

原文地址:https://www.cnblogs.com/exciting/p/11162145.html

时间: 2024-07-28 18:44:08

c++11 std::ref std::cref的相关文章

c++11 std::ref使用场景

C++本身有引用(&),为什么C++11又引入了std::ref? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: #include <functional> #include <iostream> void f(int& n1, int& n2, const int& n3) { std::cout << "In function: " << n1 <

std::ref() 与 &amp;

引言 之前因为调整样式把博客园的样式毁了,所以一直在自己的另一个博客上更新,有兴趣的可以去观望一下:http://blog.yunlambert.top/最近还是把博客园拾起来吧..... 最近看到一个多线程代码如下: typedef unsigned long long ULL; void accumulator_function(const std::vector<int> &v, ULL &acm, unsigned int beginIndex, unsigned in

c++11特性之std::thread--进阶二

继续C++11的std::thread之旅! 下面讨论如何给线程传递参数 这个例子是传递一个string #include <iostream> #include <thread> #include <string> void thread_function(std::string s) { std::cout << "thread function "; std::cout << "message is = &qu

C++11 并发指南------std::thread 详解

参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Introduction-to-Thread.md#stdthread-%E8%AF%A6%E8%A7%A3 本节将详细介绍 std::thread 的用法. std::thread 在 <thread> 头文件中声明,因此使用 std::thread 需包含 <thread> 头文件. &

[转] c++11并发之std::thread

[转自 https://blog.csdn.net/liuker888/article/details/46848905#] 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic 本文概要: 1.成员类型和成员函数. 2.std::thread 构造函数. 3.异步. 4.多线程传递参数. 5.join.detach. 6.获取CPU核心个数. 7.CPP原子变量与线程安全. 8.lambda与多线程. 9.时间等待相关问题. 10.线程功能拓展. 11.

C++11 学习笔记 std::function和bind绑定器

一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法五花八门.为了统一泛化函数对象,函数指针,引用函数,成员函数的指针的各种操作,让我们可以按更统一的方式写出更加泛化的代码,C++11推出了std::function. std::function是可调用对象的包装器.它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可调用对象.通过指定它的模板参数,它可以用统一的方式处理函数,函数对象,函数指针,并允许保存和

C++11并发之std::mutex

知识链接: C++11并发之std::thread   本文概要: 1. 头文件. 2.std::mutex. 3.std::recursive_mutex. 4.std::time_mutex. 5.std::lock_guard 与 std::unique_lock. Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 #include 头文件中,所以如果你需要使用 std::mutex,就必须包含 #include 头文件. 1. 头文件. Mute

c++11 标准库函数 std::move 和 完美转发 std::forward

c++11 标准库函数 std::move 和 完美转发 std::forward #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <vector> #include <map> // C++中还有一个被广泛认同的说法,那就是可以取地址的.有名字的就是左值,反之,不能取地址的.没有名字的就是右值. // 相对于左值,右值表示字面常量.表达式.函数的非

C++11学习笔记之三lamda表达式,std::function, std::bind

//lamda //first lamda [] {}; // second lamda []() //or no need () when paramater is null { std::cout << "second" << std::endl; }();// last add(), express will call this lamda func // 3 with return type auto kkk = []() { return 1; }()