std::async works for own type and other std types like thread, why does it fail here for std::atomic_ref ?
#include <future>
#include <iostream>
#include <atomic>
#include <thread>
struct Dummy
{
Dummy() : value(0) {}
void add(int i) { value +=i;}
int value;
};
void func(){}
int main()
{
// Dummy::add
Dummy foo;
auto f = std::async(&Dummy::add, &foo, 77);
f.get();
std::cout << foo.value << "\n";
// std::thread::join
std::thread t(func);
auto f2 = std::async(&std::thread::join, &t);
// std::atomic_ref::wait
size_t a = 0;
std::atomic_ref<size_t> atom(a);
atom.wait(1); // OK
auto f1 = std::async(&std::atomic_ref<size_t>::wait, &atom, 1); // error
}
<source>:29:23: error: no matching function for call to 'async(void (std::__atomic_ref<long unsigned int, true, false>::*)(long unsigned int, std::memory_order) const noexcept, std::atomic_ref<long unsigned int>*, int)'
>Solution :
Function pointers (including member function pointers) don’t support default arguments, they are silently ignored.
std::atomic_ref::wait has a second parameter with a default argument: std::memory_order order = std::memory_order::seq_cst, and when calling through a member-pointer, it needs to be specified manually:
auto f1 = std::async(&std::atomic_ref<size_t>::wait, &atom, 1, std::memory_order_seq_cst);