I am working with std::async
and std::future
in c++, but am having a bit of trouble.
When I run this code, i expect to see (in the stdout) "hello world"
but, instead I get nothing:
#include <iostream>
#include <future>
using namespace std;
struct A {
future<string>* test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ &a };
}
int main() {
A a = callA();
cout << a.test->get() << endl;
}
I am using a pointer to a future because in my real program, i have another struct in place of the std::string
:
struct A;
struct B;
typedef struct A {
future<B>* b;
} A;
typedef struct B {
A a;
} B;
and even if i don't use a pointer it will give me this error:
error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = std::__cxx11::basic_string<char>]'
(For the above error, i know that i can use std::move to fix it as seen here, but i need to use the pointer)
So how can i actually get the output of "hello world"
from this program?