'Example from cppreference.com does not compile with MSVC
Tried to compile the following example from cppreference.com:
#include <coroutine>
struct promise;
struct coroutine : std::coroutine_handle<promise>
{
using promise_type = struct promise;
};
struct promise {
coroutine get_return_object()
{
return { coroutine::from_promise(*this) };
}
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
struct S {
int i;
coroutine f() {
std::cout << i;
co_return;
}
};
but got compiler error:
error C3789: this function cannot be a coroutine: 'coroutine::promise' does not declare the member 'get_return_object()'
Compiler: MSVC2019, /std:c++latest
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30137 for x86
But GCC12 compiles it, what is wrong with MSVC?
EDIT1
The same error with MSVC2022
Optimizing Compiler Version 19.30.30709 for x86
/std:c++latest
Solution 1:[1]
As the Site says:
Work in progress: This page is in progress of being updated to reflect the parts of Coroutines Technical Specification that were included in the working draft of C++20.
This example is not runnable with a MSVC2019 Compiler. You can install and try it with this version:
https://docs.microsoft.com/en-us/visualstudio/releases/2022/release-notes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | D.G. |
