Note: I don't ask about how to make the below code snippet work, there are already some posts about that question. What am I conscious about is why std::async itself could not find matched function, whereas lambda could? You see the compiler complains that there is no matching function for call to
'async(std::launch, <unresolved overloaded function type>, std::shared_ptr<Demo>, int)'
, whereas lambda(i.e. [ptr=shared_from_this()](){return ptr->foo(2);) could find suitable overloading.
Why?
Here is the code snippet:
#include <future>
#include <functional>
#include <memory>
class Demo:public std::enable_shared_from_this<Demo>
{
public:
int foo(){return 0;};
int foo(int a){return 0;};
std::future<int> AsyncFoo1()
{
return std::async(std::launch::async, &Demo::foo, shared_from_this(), 2); //Why this line does not compile, whereas `AsyncFoo2()` which uses lambda works well ? I know the blow one is right.
//return std::async(std::launch::async, static_cast<int(Demo::*)(int)>(&Demo::foo), shared_from_this(), 2);
}
std::future<int> AsyncFoo2()
{
return std::async(std::launch::async, [ptr=shared_from_this()](){return ptr->foo(2);});
}
};
int main()
{
}
Here is what the compiler complains:
<source>: In member function 'std::future<int> Demo::AsyncFoo1()':
<source>:13:26: error: no matching function for call to 'async(std::launch, <unresolved overloaded function type>, std::shared_ptr<Demo>, int)'
13 | return std::async(std::launch::async, &Demo::foo, shared_from_this(), 2); //Why this line does not compile, whereas `AsyncFoo2()` which uses lambda works well ? I know the blow one is right.
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from <source>:1:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/future:1769:5: note: candidate: 'template<class _Fn, class ... _Args> std::future<typename std::__invoke_result<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...>::type> std::async(launch, _Fn&&, _Args&& ...)'
1769 | async(launch __policy, _Fn&& __fn, _Args&&... __args)
| ^~~~~
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/future:1769:5: note: template argument deduction/substitution failed:
<source>:13:26: note: couldn't deduce template parameter '_Fn'
13 | return std::async(std::launch::async, &Demo::foo, shared_from_this(), 2); //Why this line does not compile, whereas `AsyncFoo2()` which uses lambda works well ? I know the blow one is right.
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/future:1803:5: note: candidate: 'template<class _Fn, class ... _Args> std::future<typename std::__invoke_result<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...>::type> std::async(_Fn&&, _Args&& ...)'
1803 | async(_Fn&& __fn, _Args&&... __args)
| ^~~~~
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/future:1803:5: note: template argument deduction/substitution failed:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/future: In substitution of 'template<class _Fn, class ... _Args> std::future<typename std::__invoke_result<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {}]':
<source>:13:26: required from here
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/future:1803:5: error: no type named 'type' in 'struct std::__invoke_result<std::launch>'