由于类型转换问题,C++/WinRT 协程无法编译
C++ / WinRT Coroutine does not compile due to problem with type conversion
我已经开始使用 C++/WinRT,我想实现调用函数的协程 "LaunchFileAsync"。但是我的代码没有编译,我也不知道为什么。
#include <pch.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.Storage.h>
using namespace winrt;
using namespace Windows::System;
using namespace Windows::Storage;
using namespace Windows::Foundation;
Windows::Foundation::IAsyncOperation<bool> ActionOnClick()
{
const auto uri = Uri(L"URI");
const auto storageFile{ co_await StorageFile::GetFileFromApplicationUriAsync(uri) };
co_return Launcher::LaunchFileAsync(storageFile);
}
按照https://docs.microsoft.com/en-us/uwp/api/windows.system.launcher.launchfileasync?view=winrt-18362,Launcher::LaunchFileAsyncreturnsIAsyncOperation,但是编译报如下错误:
1>E:ExamplesConsoleApplication1\ConsoleApplication1\main.cpp(16,1): error C2664: "void std::experimental::coroutine_traits<winrt::Windows::Foundation::IAsyncOperation<bool>>::promise_type::return_value(const TResult &) noexcept": argument 1 cannot be converted from "winrt::Windows::Foundation::IAsyncOperation<bool>" to "TResult &&"
1> with
1> [
1> TResult=bool
1> ]
1>E:Examples\ConsoleApplication1\ConsoleApplication1\main.cpp(16,1): message : Cause: Can't be converted from "winrt::Windows::Foundation::IAsyncOperation<bool>" to "TResult"
1> with
1> [
1> TResult=bool
1> ]
1>E:\Examples\ConsoleApplication1\ConsoleApplication1\main.cpp(16,40): message : No user-defined conversion operator is available to perform this conversion or the operator cannot be called
有什么问题?
该代码试图 return 一个等待对象而不是 return 的值。您必须先 co_await
结果,然后才能 co_return
它:
Windows::Foundation::IAsyncOperation<bool> ActionOnClick()
{
const auto uri = Uri(L"URI");
const auto storageFile{ co_await StorageFile::GetFileFromApplicationUriAsync(uri) };
auto const result{ co_await Launcher::LaunchFileAsync(storageFile) };
co_return result;
}
我已经开始使用 C++/WinRT,我想实现调用函数的协程 "LaunchFileAsync"。但是我的代码没有编译,我也不知道为什么。
#include <pch.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.Storage.h>
using namespace winrt;
using namespace Windows::System;
using namespace Windows::Storage;
using namespace Windows::Foundation;
Windows::Foundation::IAsyncOperation<bool> ActionOnClick()
{
const auto uri = Uri(L"URI");
const auto storageFile{ co_await StorageFile::GetFileFromApplicationUriAsync(uri) };
co_return Launcher::LaunchFileAsync(storageFile);
}
按照https://docs.microsoft.com/en-us/uwp/api/windows.system.launcher.launchfileasync?view=winrt-18362,Launcher::LaunchFileAsyncreturnsIAsyncOperation,但是编译报如下错误:
1>E:ExamplesConsoleApplication1\ConsoleApplication1\main.cpp(16,1): error C2664: "void std::experimental::coroutine_traits<winrt::Windows::Foundation::IAsyncOperation<bool>>::promise_type::return_value(const TResult &) noexcept": argument 1 cannot be converted from "winrt::Windows::Foundation::IAsyncOperation<bool>" to "TResult &&"
1> with
1> [
1> TResult=bool
1> ]
1>E:Examples\ConsoleApplication1\ConsoleApplication1\main.cpp(16,1): message : Cause: Can't be converted from "winrt::Windows::Foundation::IAsyncOperation<bool>" to "TResult"
1> with
1> [
1> TResult=bool
1> ]
1>E:\Examples\ConsoleApplication1\ConsoleApplication1\main.cpp(16,40): message : No user-defined conversion operator is available to perform this conversion or the operator cannot be called
有什么问题?
该代码试图 return 一个等待对象而不是 return 的值。您必须先 co_await
结果,然后才能 co_return
它:
Windows::Foundation::IAsyncOperation<bool> ActionOnClick()
{
const auto uri = Uri(L"URI");
const auto storageFile{ co_await StorageFile::GetFileFromApplicationUriAsync(uri) };
auto const result{ co_await Launcher::LaunchFileAsync(storageFile) };
co_return result;
}