gpt4 book ai didi

c++ - co_await 不工作

转载 作者:行者123 更新时间:2023-11-27 23:59:20 29 4
gpt4 key购买 nike

我尝试尝试本文中提到的 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.0\VC\include\experimental\resumable(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.0\VC\include\experimental\resumable(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.0\VC\include\experimental\resumable(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.0\VC\include\experimental\resumable(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.0\VC\include\experimental\resumable(44,0): Error C2061: syntax error: identifier 'promise_type' (compiling source file MainPage.xaml.cpp)
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\experimental\resumable(44): error C2061: syntax error: identifier 'promise_type' (compiling source file MainPage.xaml.cpp)
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\experimental\resumable(44,0): Error C2238: unexpected token(s) preceding ';' (compiling source file MainPage.xaml.cpp)

本来,我把下面的代码放在构造函数中

#include <experimental\resumable>
#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");
}

导致上述编译错误。

最佳答案

我的猜测是,第一个问题是您无法返回 voidvoid 以来的可恢复函数不填充 co_await 的任何协程特征期待(如 get_return_objectset_result 等)

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

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

关于c++ - co_await 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40369163/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com