co_await not working

co_await not working

本文关键字:working not await co      更新时间:2023-10-16

我尝试了本文中提到的co_await https://blogs.msdn.microsoft.com/vcblog/2016/04/04/using-c-coroutines-to-simplify-async-uwp-code/,但是出现了奇怪的编译错误:

C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44,0): Error C2825: '_Ret': must be a class or namespace when followed by '::' (compiling source file MainPage.xaml.cpp)
C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44): error C2825: '_Ret': must be a class or namespace when followed by '::' (compiling source file MainPage.xaml.cpp) 
MainPage.xaml.cpp(44): note: see reference to class template instantiation 'std::experimental::coroutine_traits<void,::MainPage ^,Windows::UI::Xaml::Navigation::NavigationEventArgs ^>' being compiled
C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44,0): Error C2510: '_Ret': left of '::' must be a class/struct/union (compiling source file MainPage.xaml.cpp)
C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44): error C2510: '_Ret': left of '::' must be a class/struct/union (compiling source file MainPage.xaml.cpp)
C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44,0): Error C2061: syntax error: identifier 'promise_type' (compiling source file MainPage.xaml.cpp)
C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44): error C2061: syntax error: identifier 'promise_type' (compiling source file MainPage.xaml.cpp)
C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeexperimentalresumable(44,0): Error C2238: unexpected token(s) preceding ';' (compiling source file MainPage.xaml.cpp)

最初,我将以下代码放入构造函数

#include <experimentalresumable>
#include <pplawait.h>
using namespace concurrency;
MainPage::MainPage()
{
    InitializeComponent();
    auto my_data_file = co_await Windows::ApplicationModel::Package::Current->InstalledLocation->GetFileAsync("samples.txt");
    // Preparing app data structures
}

不起作用。我猜这在构造函数中是不可能的(原因很明显),所以我将co_await行移动到

void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
    auto my_data_file = co_await Windows::ApplicationModel::Package::Current->InstalledLocation->GetFileAsync("samples.txt");
}

我的猜测是,第一个问题是你不能从一个可恢复的函数返回void,因为void不填补co_await期望的任何协程特征(如get_return_object, set_result等)

如果您已经在使用PPL,返回task<void>:

task<void> MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
    auto my_data_file = co_await Windows::ApplicationModel::Package::Current->InstalledLocation->GetFileAsync("samples.txt");
}

我找到了答案:只有当我们返回task<> 时才能使用co_await(这是不正确的,请参阅@DavidHaim的答案)。我猜这只是create_task的语法糖。在这种情况下,解决方案是提取一个返回task<void>的方法:

#include <experimentalresumable>
#include <pplawait.h>
using namespace concurrency;
MainPage::MainPage()
{
    InitializeComponent();
    PrepareData();
}
task<void> MainPage::PrepareData()
{
     auto my_data_file = co_await Windows::ApplicationModel::Package::Current->InstalledLocation->GetFileAsync("samples.txt");
     // Preparing app data structures
}

一些注释:虽然协程特性看起来不错,但它污染了语言。观察PrepareData()的主体没有任何return语句,但签名说它返回task<void>,导致我无法识别文章中的要求。