我正在尝试异步评估我的代码的一部分
\n\n#include <stdio.h>\n#include <string>\n#include <memory>\n#include <future>\n#include <map>\n\nnamespace IG\n{\n typedef std::map<uint, std::string> CadDef;\n\n class FooFoo\n {\n CadDef foo()\n {\n CadDef cdef{};\n cdef[1] = "aa";\n return cdef;\n }\n };\n}\n\nint main()\n{\n auto ptr = std::make_unique<IG::FooFoo>();\n std::future<IG::CadDef> resFut = std::async(ptr->foo);\n auto res = resFut.get();\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但代码无法编译 -\n(在 gcc 上)
\n\nerror: invalid use of non-static member function \xe2\x80\x98IG::CadDef IG::FooFoo::foo()\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n(在 msvc 上——我的主程序,我从中提取了最小的示例)
\n\nerror C3867: \'IG::FooFoo::foo\': non-standard syntax; use \'&\' to create a pointer to member\nerror C2672: \'std::async\': no matching overloaded function found\nerror C2780: \'std::future<_Invoke_traits<void,decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)\': expects 3 arguments - 1 provided\nRun Code Online (Sandbox Code Playgroud)\n\n似乎 MSVC 抱怨我没有使用过,ptr->foo()但我不确定。
我究竟做错了什么?
\n您可以使用如下所示的 lambda,使用策略*
auto handle = std::async(std::launch::async, [&ptr](){
return ptr->foo(); // Ofcourse make foo public in your snippet
});
auto res = handle.get();
Run Code Online (Sandbox Code Playgroud)
*不一定需要