c++ - 使用 std::async 调用函数时,引用变量的地址不同
问题描述
在下面的代码片段中,v
in函数的地址与 in使用的when
的print
地址相同,但传递的 when 不同a
std::ref(a)
std::async
a
#include <iostream>
#include <future>
void print (const int& v)
{
std::cout << "&v = " << &v << "\n";
}
int main()
{
int a = 10;
auto f = std::async(print, std::ref(a));
f.get();
std::cout << "&a = " << &a << "\n";
return 0;
}
输出
&v = 0x7ffd3b5000b4
&a = 0x7ffd3b5000b4
但是当我删除时std::ref
,地址是不同的
auto f = std::async(print, a);
输出
&v = 0x55586497cef8
&a = 0x7ffee2e3dadc
问题:
- 如果不使用,是否
std::async
复制数据而不是参考std::ref
? - 即使我的
print
函数参数是引用类型,为什么地址会发生变化const int &
解决方案
推荐阅读
- json - 如何使用 Fluentd 嵌套 json 日志?
- android - Kotlin Android MVVM RecyclerView 不显示任何数据
- javascript - 如何解决布尔诺言
- c# - 如何使用 IsFaulted、IsCanceled 和 IsCompleted 测试用例测试“ToListAsync”
- sql - 32 位 ODBC 驱动程序连接问题
- html - 使用 XPath 将 HTML 中的信息提取/解析到 Google 表格中
- c++ - 如何添加预防措施以绕过未注册的 Microsoft.ACE.OLEDB.12.0?
- jquery - 无法从 Zend Framework 3 向 Ajax 调用发送 JSON 响应
- c# - Entity Framework core 3.1.1 多级继承
- docker - 通过 Docker 日志记录数据 - .NET Core 3.1 Filebeat ELK