async with member function
asynchronous, c++, c++11
Solution
`cap.pr` is an incomplete member function call expression. You must follow it with parentheses containing the appropriate function arguments to make a valid C++ expression.
You can't therefore pass `cap.pr` to `std::async` or any other function.
To pass a member function to `std::async` you need to use the syntax you found:
auto f=std::async(&capc::pr,cap);
Though in this case, you need to be aware that the `cap` object is copied. You could also use
auto f=std::async(&capc::pr,&cap);
to just pass a pointer to `cap`.
If the pointer-to-member-function syntax is unwelcome then you can use a lambda:
auto f=std::async([&]{cap.pr();});
This isn't quite the same: it doesn't pass the member function pointer and object pointer to `std::async`, it passes a lambda object containing a reference to `cap` that calls its `pr` member function directly. However, the result is essentially the same.
Problem
I've a code: ``` class cabc{ public: void pr() { cout<<"abcdef"; } }; int main() { cabc cap; auto f = async(cap.pr); f.get(); return 0; } ``` This code is not working. I know the same thing can be done using: ``` auto f = async(&cabc::pr,cap); ``` This is working. But why the first approach is not working?