Skip to content

Windows.Foundation await adapters do not compile with C++20 standard coroutines #764

@kobykahane

Description

@kobykahane

With cppwinrt 2.0.201008.2, the following does not compile with Visual Studio 2019 16.8 Preview 3.2 with /std:c++latest:

#include <coroutine>
#include <winrt/base.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Devices.Bluetooth.h>

using namespace winrt;

using winrt::Windows::Foundation::IAsyncOperation;

IAsyncOperation<Windows::Devices::Bluetooth::BluetoothLEDevice> foo()
{
    auto bluetoothAdapter = co_await Windows::Devices::Bluetooth::BluetoothAdapter::GetDefaultAsync();
    co_return nullptr;
}

with the error:

...Generated Files\winrt\base.h(8415,46): error C2338: Not an awaitable type
...Generated Files\winrt\base.h(8460,30): error C2039: 'await_resume': is not a member of 'winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Devices::Bluetooth::BluetoothAdapter>'

If the build is reverted to legacy coroutines by passing /await and including <experimental/coroutine> instead, the code compiles successfully.

The problem appears to be that the various co_await operators in Windows.Foundation.h have the following conditional:

#if defined(__cpp_coroutines)

which is the feature-test macro for the legacy Coroutines TS, rather than also including __cpp_lib_coroutine. Furthermore, the adapters themselves may need some adjustments, although I have not looked into that.
There appear to be a few other places that still only test for the Coroutines TS macro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions