0

I don't understand how the following call of std async is unable to compile and the error message doesn't make much sense to me:

#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;
}

the above code compiled with MSVC on c++20 outputs the following:

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.vcxp
roj]
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 &&,_Arg 
Types &&...)' [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.vcxpro 
j]

Sorry for the lack of detail but I'm truly lost on what's going wrong here, and the error message doesn't enlighten me much

wigi426
  • 91
  • 4

0 Answers0