co_await not working
co_await not working
我尝试了本文中提到的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>
,导致我无法识别文章中的要求。
相关文章:
- GDB - strcmp not working: __strcmp_sse2_unaligned
- QT qScriptConnect is not working
- QDesktopServices::openUrl() not working Qt
- "Configure: Error: Could Not Find a Working Compiler, See config.log For Details"
- C++ std::set ordering not working with customer class*
- C++ SetWindowText not working
- visual studio 2012 c++ hello world - iostream not working
- getGravity not working?
- Opencv findcontours CV_RETR_EXTERNAL not working
- c++ srand() not working
- std::queue::empty() not working?
- BCB swprintf not working
- 将类型作为指向另一个类的对象的指针的泛型类-NOT WORKING
- LoadLibrary is not working
- gluUnProject not working
- C++ catch bad_alloc not working
- updatelayeredwindow not working
- OpenMP set_num_threads() is not working
- glOrtho not working
- Cuda atomicInc not working