thread - 传递引用参数
当给 thread 的执行函数传递指针参数时,没有任何问题,但是如果想传递引用,按照普通函数的调用方法会遇到编译失败:
#include <iostream>
#include <thread>
#include <string> int main()
{
std::string a("Hello");
std::cout << "address = " << &a << "\n";
std::thread t([](std::string& a)
{
std::cout << "in thread address = " << &a << "\n";
}, a);
t.join();
}
编译:g++ -std=c++11 -pthread test.cpp
失败了:
In file included from /usr/include/c++/4.7/bits/move.h::,
from /usr/include/c++/4.7/bits/stl_pair.h:,
from /usr/include/c++/4.7/bits/stl_algobase.h:,
from /usr/include/c++/4.7/bits/char_traits.h:,
from /usr/include/c++/4.7/ios:,
from /usr/include/c++/4.7/ostream:,
from /usr/include/c++/4.7/iostream:,
from test.cpp::
/usr/include/c++/4.7/type_traits: In instantiation of ?.truct std::_Result_of_impl<false, false, main()::<lambda(std::string&)>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >?.
/usr/include/c++/4.7/type_traits::: required from ?.lass std::result_of<main()::<lambda(std::string&)>(std::basic_string<char>)>?
/usr/include/c++/4.7/functional::: required from ?.truct std::_Bind_simple<main()::<lambda(std::string&)>(std::basic_string<char>)>?
/usr/include/c++/4.7/thread::: required from ?.td::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(std::string&)>; _Args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]?
test.cpp::: required from here
/usr/include/c++/4.7/type_traits::: error: no match for call to ?.main()::<lambda(std::string&)>) (std::basic_string<char>)?
test.cpp::: note: candidates are:
In file included from /usr/include/c++/4.7/bits/move.h::,
from /usr/include/c++/4.7/bits/stl_pair.h:,
from /usr/include/c++/4.7/bits/stl_algobase.h:,
from /usr/include/c++/4.7/bits/char_traits.h:,
from /usr/include/c++/4.7/ios:,
from /usr/include/c++/4.7/ostream:,
from /usr/include/c++/4.7/iostream:,
from test.cpp::
/usr/include/c++/4.7/type_traits::: note: void (*)(std::string&) {aka void (*)(std::basic_string<char>&)} <conversion>
/usr/include/c++/4.7/type_traits::: note: candidate expects arguments, provided
test.cpp::: note: main()::<lambda(std::string&)>
test.cpp::: note: no known conversion for argument from ?.td::basic_string<char>?.to ?.td::string& {aka std::basic_string<char>&}?
这里类似于 std::bind,std::thread 和 std::bind 采用了相同的机制,必须使用 std::ref 来包装:
#include <iostream>
#include <thread>
#include <string> int main()
{
std::string a("Hello");
std::cout << "address = " << &a << "\n";
std::thread t([](std::string& a)
{
std::cout << "in thread address = " << &a << "\n";
}, std::ref(a));
t.join();
}
thread - 传递引用参数的更多相关文章
- java方法强制传递引用参数(做为返回值),改变被传递参数值。
Java传递参数分为2种: 值类型,Java里面也叫简单类型,这种参数类型的传递的是它的副本拷贝: 引用类型,传递的是对象引用地址,如果在方法内改变该参数对象属性即是对原引用对象的改变:如果不想这样传 ...
- boost和std中的thread的引用参数
boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...
- 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用
原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...
- Java 给Thread传递参数
一开始我想把run()函数写成有参函数来传值,后来发现行不通.经过查找,最终用如下方法传递了参数: 也就是用另外一个有参函数setTar()传递参数. 调用的时候用这4行code传递参数: 上面是用i ...
- Web报表页面如何传递中文参数
1.场景描述 在用报表开发工具FineReport设计的web报表中,给iframe设置src嵌入某个报表时,往往会给报表传递初始的参数值,例如: <iframe id="report ...
- 传递引用类型参数的两种方式(转自 MSDN)
引用类型的变量不直接包含其数据:它包含的是对其数据的引用.当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值(更改属性的值),但是无法更改引用本身的值:也就是说,不能使用相同的引 ...
- 传递引用类型参数(ref)
引用类型的变量不直接包含其数据:它包含的是对其数据的引用. 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值. 但是无法更改引用本身的值:也就是说,不能使用相同的引用为新类分配 ...
- 实例对比剖析c#引用参数的用法
c#引用参数传递的深入剖析值类型的变量存储数据,而引用类型的变量存储对实际数据的引用.(这一点很重要,明白了之后就能区分开值类型和引用类型的差别) 在参数传递时,值类型是以值的形式传递的(传递的是值, ...
- C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数
方法的参数有六种,分别是值参数.引用参数.输出参数.参数数组.命名参数.可选参数. 值参数 值参数是方法的默认类型,通过复制实参的值到形参的方式把数据传递到方法,方法被调用时,系统作两步操作: 在栈中 ...
随机推荐
- 使用Spring表达式语言进行装备--SpEL
本文主要想记录最近的两个使用spring框架实现通过配置文件装备Bean,以及使用SpEL装备Bean. 1.使用配置文件装备Bean: 当我们写某些Bean的时候是希望这个Bean当中的属性是可以通 ...
- Python学习之路——装饰器
开放封闭原则:不改变调用方式与源代码上增加功能 ''' 1.不能修改被装饰对象(函数)的源代码(封闭) 2.不能更改被修饰对象(函数)的调用方式,且能达到增加功能的效果(开放) ''' 装饰器 # 把 ...
- JS异常
当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错误. 可能是语法错误,通常是程序员造成的编码错误或错别字. 可能是拼写错误或语言中缺少的功能(可能由于浏览器差异). 可能 ...
- windows 下搭建 git 服务器 gogs
本文基于 windows7 64位 搭建 gogs gogs 官方文档地址:https://gogs.io/docs软件下载地址:https://dl.gogs.io/ 环境要求 数据库(选择以下一项 ...
- sqlite 数据库 boolean类型的小小测试
根据官方文档的介绍: SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored ...
- Codeforces 1082B Vova and Trophies(前缀+后缀)
题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...
- beam 的异常处理 Error Handling Elements in Apache Beam Pipelines
Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...
- 用servlet校验密码2
首先,mysql真的让我有点扎心,虽然安装了但是之前没用过 第一个 初始密码给我设了fj4X1=).......一长串字符,怎么记得住嘛,再说,我记那玩意儿干啥呀 所以 果断决定改个不费脑子的密码 但 ...
- python django(forms组件)
forms组件最大的作用,就是做数据校验. 普通做法,一个一个写校验规则,没有解耦.校验规则,都在视图函数里面. 网页校验 修改urls.py,增加路径addbook from app01 impor ...
- EF CodeFirst系列(8)--- FluentApi配置单个实体
我们已经知道了在OnModelCreating()方法中可以通过FluentApi对所有的实体类进行配置,然而当实体类很多时,我们把所有的配置都放在OnModelCreating()方法中很难维护.E ...