{"id":105882,"date":"2021-11-08T07:00:00","date_gmt":"2021-11-08T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105882"},"modified":"2021-11-08T06:50:02","modified_gmt":"2021-11-08T14:50:02","slug":"20211108-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211108-00\/?p=105882","title":{"rendered":"Restoring support for pre-standardization coroutine free awaiters for even older code bases"},"content":{"rendered":"<p>A customer had a large code base that was originally written in C++\/CX, and they have been gradually converting it to C++\/WinRT. They managed to get most of it converted, but the XAML binding is a major obstacle because you have to do it all at once, and they have a lot of XAML.<\/p>\n<p>They found that with the upgrade to version 2.0.200729.8 of C++\/WinRT, they lost the ability to <code>co_await<\/code> an <code>IAsyncAction^<\/code>. What they discovered is that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200916-00\/?p=104227\"> C++\/WinRT dropped support for pre-standardization coroutine free awaiters<\/a>. They tried the modernizer from the article, but couldn&#8217;t get it to work.<\/p>\n<p>Okay, the first mistake is mine. I inadvertently relied upon a C++20 feature in the <code>make_<wbr \/>cpp20_<wbr \/>await_<wbr \/>adapter<\/code> function:<\/p>\n<pre>    template&lt;typename Awaitable&gt;\r\n    auto make_cpp20_await_adapter(Awaitable&amp; awaitable)\r\n    {\r\n        return cpp20_await_adapter&lt;Awaitable&gt;(awaitable);\r\n    }\r\n<\/pre>\n<p>This takes advantage of the treatment of a parenthesized list as an aggregate initializer if a class lacks a constructor. To get this code to work on C++17, we&#8217;ll have to use the braced constructor instead.<\/p>\n<pre>    template&lt;typename Awaitable&gt;\r\n    auto make_cpp20_await_adapter(Awaitable&amp; awaitable)\r\n    {\r\n        return cpp20_await_adapter&lt;Awaitable&gt;<span style=\"color: blue;\">{<\/span> awaitable <span style=\"color: blue;\">}<\/span>;\r\n    }\r\n<\/pre>\n<p>I&#8217;ve retroactively updated the article to incorporate this fix.<\/p>\n<p>The last piece of the puzzle is realizing that some old versions of <code>pplawait.h<\/code> use free awaiters and therefore require modernization. (Version 14.16.27023 uses free awaiters, but version 14.29.30133 uses standard awaiters. I don&#8217;t know exactly when the change happened.)<\/p>\n<p>If you can&#8217;t upgrade to a newer version of <code>pplawait.h<\/code>, you can add modernizers:<\/p>\n<pre>namespace Windows::Foundation\r\n{\r\n    auto operator co_await(IAsyncAction^ x)\r\n    {\r\n        return ::modernizer::make_cpp20_await_adapter(x);\r\n    }\r\n\r\n    template&lt;typename T&gt;\r\n    auto operator co_await(IAsyncOperation&lt;T&gt;^ x)\r\n    {\r\n        return ::modernizer::make_cpp20_await_adapter(x);\r\n    }\r\n\r\n    template&lt;typename P&gt;\r\n    auto operator co_await(IAsyncActionWithProgress&lt;P&gt;^ x)\r\n    {\r\n        return ::modernizer::make_cpp20_await_adapter(x);\r\n    }\r\n\r\n    template&lt;typename T, typename P&gt;\r\n    auto operator co_await(IAsyncOperationWithProgress&lt;T, P&gt;^ x)\r\n    {\r\n        return ::modernizer::make_cpp20_await_adapter(x);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Connecting the dots, and reaching back to an earlier time.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-105882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Connecting the dots, and reaching back to an earlier time.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105882"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105882\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}