The other day, I was writing some code, and wanted to do something like this:
#include <future>
#include <iostream>
class AsyncTesting {
public:
void callAsyncFunction() {
bool testingBool = false;
std::future<bool> inversedBool = std::async(&AsyncTesting::inverseBoolAsync, this, testingBool);
// do stuff with my bool (just print it for testing purposes)
std::cout << inversedBool.get() << std::endl;
}
bool inverseBoolAsync(bool& arg) {
return (!arg);
}
};
int main() {
AsyncTesting testObj;
testObj.callAsyncFunction();
return 0;
}
But upon compiling (g++), I get this string of errors:
https://pastebin.com/cmwy28FZ
I understand that having the same variable being overwritten at the same time could be problematic, but I'm guaranteed not to use the variable before I future.get() it. Is there any way to tell the compiler this, or is this code just inherently problematic & I just need to use pointers here (because they seem to work, just are not personally preferred)
P.S. Sorry if I formatted anything incorrectly or used wrong tags, I'm new here!