{"id":107582,"date":"2022-12-12T07:00:00","date_gmt":"2022-12-12T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107582"},"modified":"2022-12-12T06:45:59","modified_gmt":"2022-12-12T14:45:59","slug":"20221212-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221212-00\/?p=107582\/","title":{"rendered":"What does it mean when the compiler tells me that <CODE>promise_type<\/CODE>: is not a member of <CODE>coroutine_traits&lt;void&gt;<\/CODE>?"},"content":{"rendered":"<p>A customer using C++\/WinRT found that their attempt to perform a <code>co_await<\/code> on an <code>IAsyncAction<\/code> failed to compile with the error<\/p>\n<pre style=\"white-space: pre-wrap;\">promise_type: is not a member of coroutine_traits&lt;void&gt;\r\n<\/pre>\n<p>What does this error mean? &#8220;We included <code>&lt;winrt\/Windows.Foundation.h&gt;<\/code>, which is allegedly <a title=\"Why am I getting a weird error about promise_type when I try to write a coroutine?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210809-00\/?p=105539\"> how to fix this weird error message<\/a>, but that didn&#8217;t fix it.&#8221;<\/p>\n<p>This is a case of skipping the <i>reading the explanation<\/i> part of looking for a solution to your problem and skipping directly to the <i>how to fix it<\/i> part, without checking that the <i>how to fix it<\/i> applies to your specific situation. &#8220;Somebody called in to a car repair show because their car didn&#8217;t start, and the answer was that they had loose battery cables. My car won&#8217;t start, and I tightened the battery cables, but it didn&#8217;t fix it.&#8221; Well yeah, because you skipped over the part of the call where they did the troubleshooting.<\/p>\n<p>As I noted in the earlier article, the <code>co_await<\/code> keyword triggers the coroutine transformation which rewrites the function performing the <code>co_await<\/code> into a state machine, and one of the pieces of the state machine is the <code>promise_<wbr \/>type<\/code> associated with the coroutine&#8217;s return type and parameters.<\/p>\n<p>The <code>coroutine_<wbr \/>traits<\/code> specialization in the error message is <code>coroutine_traits&lt;void&gt;<\/code>, so the return type is <code>void<\/code> and there are no parameters. In other words, the function performing the <code>co_await<\/code> looks something like this:<\/p>\n<pre>void DoSomething()\r\n{\r\n    co_await MyFunctionAsync();\r\n}\r\n<\/pre>\n<p>The C++\/WinRT library provides support for producing coroutines returning <code>winrt::<wbr \/>Windows::Foundation::<wbr \/>IAsyncAction<\/code> and related interfaces, as well as the special type <code>winrt::<wbr \/>fire_<wbr \/>and_<wbr \/>forget<\/code>. It does not, however, add support for coroutines returning <code>void<\/code>.<\/p>\n<p>The <code>winrt\/<wbr \/>Windows.<wbr \/>Foundation.<wbr \/>h<\/code> header file defines the Windows Runtime coroutines, but <code>void<\/code> is not a Windows Runtime coroutine type, so including Windows Runtime headers isn&#8217;t going to help. The code probably meant for <code>Do\u00adSomething()<\/code> to return a type that can be used for coroutines, most likely <code>IAsyncAction<\/code> or <code>fire_<wbr \/>and_<wbr \/>forget<\/code>.<\/p>\n<p>Now, you could always write your own support for producing coroutines returning <code>void<\/code>, say, by having it behave roughly the same as <code>fire_<wbr \/>and_<wbr \/>forget<\/code>. It&#8217;s not hard.<\/p>\n<p>But probably not advisable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Applying your understanding of the coroutine transformation.<\/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-107582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Applying your understanding of the coroutine transformation.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107582","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=107582"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107582\/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=107582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}