`yield` keyword to become `co_yield` in VS 2017
Coroutines—formerly known as “C++ resumable functions”—are one of the Technical Specifications (TS) that we have implemented in the Visual C++ compiler. We’ve supported coroutines for three years—ever since the VC++ November 2013 CTP release.
If you’re using coroutines you should be aware that the keyword `yield` is being removed in the release of VS 2017. If you use `yield` in your code, you will have to change your code to use the new keyword `co_yield` instead. If you have generators that use `yield expr`, these need to be changed to say `co_yield expr`.
As long as you’re changing your code you might want to migrate from using `await` to `co_await` and from `return` in a coroutine to `co_return`. The Visual C++ compiler accepts all three new keywords today.
For more information about coroutines, please see the Coroutines TS here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4628.pdf. As the author of the Coroutines TS works on the Visual C++ team you can also just send us mail with your questions or feedback (see below.)
Why are we making this change?
As a Technical Specification, coroutines have not yet been adopted into the C++ Standard. When the Visual C++ team implemented them in 2013, the feature was implemented as a preview of an up-and-coming C++ feature. The C++ standards committee voted in October of 2015 to change the keywords to include the prefix `co_`. The committee didn’t want to use keywords that would conflict with variable names already in use. `yield`, for example, is used widely in agricultural and financial applications. Also, there are library uses of functions called `yield` in the Ranges TS and in the thread support library.
For reference, here are the keyword mappings that need to be applied to your code.
|Instead of `await`||Use `co_await`|
|Instead of `return`||Use `co_return`|
|Instead of `yield`||Use `co_yield`|
We’re removing the `yield` keyword with VS 2017 because we’re also implementing the Range-v3 TS and we expect many developers to call `yield` after a using declaration for ranges, e.g., `using namespace ::ranges`.
Preventing these breaks in the future
We know many of you have taken dependencies on coroutines in your code and understand that this kind of breaking change is difficult. We can’t keep the committee from making changes (trust us, we try!) but at least we can do our best to make sure that you’re not surprised when things do change.
We created a new compiler switch, `/experimental` when we implemented the Modules TS in VS 2015 Update 1. You need to include `/experimental:module` on your command line so that it is clear the feature is experimental and subject to change. If we could go back in time we would have had coroutines enabled with `/experimental:await` instead of just `/await` (or `experiemental:coroutine` if we’d known what the feature would be called three years later!)
In a future release we will deprecate the `await` keyword as well as restrict the use of `return` from coroutines in favor of the new keywords `co_await` and `co_return`.
As always, we welcome your feedback. Please give us feedback about coroutines in the comments below or through e-mail at firstname.lastname@example.org.
If you encounter other problems with Visual C++ in VS 2017 please let us know via the Report a Problem option, either from the installer or the Visual Studio IDE itself. For suggestions, let us know through UserVoice. Thank you!