In the following snippet of code, the address of v
in print
function is same as address of a
when std::ref(a)
is used in std::async
, but different when a
is passed
#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;
}
output
&v = 0x7ffd3b5000b4
&a = 0x7ffd3b5000b4
But when I remove std::ref
then address is different
auto f = std::async(print, a);
output
&v = 0x55586497cef8
&a = 0x7ffee2e3dadc
Question:
- Do
std::async
copies the data rather than reference ifstd::ref
is not used ? - why addresses is changes even though my
print
function parameter is reference typeconst int &
Reference: Passing arguments to std::async by reference fails
Aucun commentaire:
Enregistrer un commentaire