std::async编译报错:无匹配重载函数问题求助
std::async编译失败问题解析
问题代码
#include <iostream> #include <future> #include <Windows.h> void someFunc(HANDLE hndl, std::ostream& outStream) { //do stuff return; } int main() { std::ostream consoleOut(std::cout.rdbuf()); HANDLE fileHndl = CreateFileA("hello.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); auto future = std::async(std::launch::async, &someFunc, fileHndl, consoleOut); future.wait(); return 0; }
编译错误信息
MSBuild version 17.3.1+2badb37d1 for .NET Framework main.cpp main.cpp(15,82): error C2672: 'std::async': no matching overloaded function found [main.vcxproj] future(1535,75): message : could be 'std::future<_Select_invoke_traits<decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(_Fty &&,_ArgTypes &&...)' [main.vcxproj] main.cpp(15,24): message : Failed to specialize function template 'std::future<_Select_invoke_traits<decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(_Fty &&,_ArgTypes &&...)' [main.vcxproj] future(1535): message : see declaration of 'std::async' [main.vcxproj] main.cpp(15,82): message : With the following template arguments: [main.vcxproj] main.cpp(15,82): message : '_Fty=std::launch' [main.vcxproj] main.cpp(15,82): message : '_ArgTypes={void (__cdecl *)(HANDLE,std::ostream &), HANDLE &, std::ostream &}' [main.vcxproj] future(1522,75): message : or 'std::future<_Select_invoke_traits<decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)' [main.vcxproj] main.cpp(15,24): message : Failed to specialize function template 'std::future<_Select_invoke_traits<decay<_Ty>::type,decay<_ArgTypes>::type...>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)' [main.vcxproj] future(1522): message : see declaration of 'std::async' [main.vcxproj] main.cpp(15,82): message : With the following template arguments: [main.vcxproj] main.cpp(15,82): message : '_Fty=void (__cdecl *)(HANDLE,std::ostream &)' [main.vcxproj] main.cpp(15,82): message : '_ArgTypes={HANDLE &, std::ostream &}' [main.vcxproj] main.cpp(17,11): error C3536: 'future': cannot be used before it is initialized [main.vcxproj]
问题原因
核心问题在于**std::async对传递的参数会执行std::decay类型转换**,这会导致两个关键问题:
std::ostream类禁用了拷贝构造函数,直接传递consoleOut对象会触发拷贝失败;- 即使忽略拷贝问题,
std::decay会移除参数的左值引用属性,无法匹配someFunc中std::ostream&的引用参数要求,导致函数模板特化失败。
解决方案
使用std::ref包装需要传递引用的参数,std::ref会生成一个可拷贝的std::reference_wrapper对象,在调用目标函数时会自动转换回原对象的引用,完美匹配参数类型。
修正后的代码:
#include <iostream> #include <future> #include <Windows.h> void someFunc(HANDLE hndl, std::ostream& outStream) { //do stuff return; } int main() { std::ostream consoleOut(std::cout.rdbuf()); HANDLE fileHndl = CreateFileA("hello.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // 用std::ref包装consoleOut,传递引用而非拷贝 auto future = std::async(std::launch::async, &someFunc, fileHndl, std::ref(consoleOut)); future.wait(); return 0; }
错误信息解读
编译器提示"no matching overloaded function found",本质是std::async处理consoleOut参数时无法完成拷贝或类型匹配,导致模板函数无法生成有效实例。后续的C3536错误只是future变量未初始化就被使用的连锁反应,解决第一个问题后会自动消失。
内容的提问来源于stack exchange,提问作者wigi426
相关产品推荐
相关产品推荐

