{"id":107958,"date":"2023-03-22T07:00:00","date_gmt":"2023-03-22T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107958"},"modified":"2023-03-21T21:35:44","modified_gmt":"2023-03-22T04:35:44","slug":"20230322-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230322-00\/?p=107958","title":{"rendered":"Why am I getting a weird error about <CODE>promise_type<\/CODE> when I try to write a coroutine? part 2"},"content":{"rendered":"<p>A customer was having trouble getting their coroutine to compile. They shared this fragment:<\/p>\n<pre>    switch (args.Key())\r\n    {\r\n    case winrt::VirtualKey::A:\r\n    {\r\n        winrt::ContentDialog dlg;\r\n        dlg.Title(winrt::box_value(L\"Title of my dialog\"));\r\n        dlg.Content(winrt::box_value(L\"Text of the dialog\"));\r\n        dlg.PrimaryButtonText(L\"Click this guy\");\r\n        dlg.SecondaryButtonText(L\"Not this guy\");\r\n        auto result = <span style=\"border-bottom: 1px dotted red; padding: 1px;\"><span style=\"border-bottom: 1px dotted red;\">co_await<\/span><\/span> dlg.ShowAsync();\r\n\r\n        switch (result)\r\n        {\r\n            ...\r\n        }\r\n    }\r\n    break;\r\n    }\r\n<\/pre>\n<p>The error that resulted from the <code>co_await<\/code> was<\/p>\n<pre style=\"white-space: pre-wrap;\">error C2039: 'promise_type': is not a member of 'std::coroutine_traits&lt;<wbr \/>void,<wbr \/>winrt::<wbr \/>Contoso::<wbr \/>implementation::<wbr \/>AwesomeControl &amp;,<wbr \/>winrt::<wbr \/>Windows::<wbr \/>UI::<wbr \/>Xaml::<wbr \/>Input::<wbr \/>KeyRoutedEventArgs&gt;'\r\n<\/pre>\n<p>They found <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\"> an earlier discussion of this error<\/a> and reported that the recommended fix did not help. They were already including <code>winrt\/Windows.Foundation.h<\/code>, but the error persisted.<\/p>\n<p>Recall that <a title=\"C++ coroutines: Basic implementation of a promise type\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210330-00\/?p=105019\"> when you <code>co_await<\/code>, the compiler looks for a <code>coroutine_traits&lt;<wbr \/>T, ...&gt;::<wbr \/>promise_type<\/code><\/a>, where <code>T<\/code> is the return type of the function, and the other stuff is rarely of any significance.<\/p>\n<p>The customer skipped over the &#8220;How to understand the error message&#8221; part and went right to &#8220;What did the error message mean in this case?&#8221; part, even though the case under study didn&#8217;t match their problem.<\/p>\n<p>It&#8217;s like listening to a radio car repair call-in show, hearing somebody say, &#8220;My car won&#8217;t start,&#8221; ignoring all of the analysis from the hosts, and skipping to the part where the host says &#8220;It sounds like you need new spark plugs.&#8221; Your car doesn&#8217;t start, so you replace the spark plugs, but it doesn&#8217;t help. What you skipped was the back-and-forth between the host and the caller to figure out that the problem was the spark plugs.<\/p>\n<p>Instead, let&#8217;s apply the discussion of the problem (rather than its conclusion). The problem is that we couldn&#8217;t find <code>coroutine_traits&lt;<wbr \/><span style=\"border: solid 1px black;\">void<\/span>, ...&gt;::<wbr \/>promise_type<\/code>.<\/p>\n<p>And that&#8217;s the crux of the problem.<\/p>\n<p>Nobody has defined what it means to create a coroutine from a function that returns <code>void<\/code>. Including <code>winrt\/<wbr \/>Windows.<wbr \/>Foundation.h<\/code> won&#8217;t help. That header file defines how to create a coroutine from a function that returns <code>IAsyncAction<\/code>, <code>IAsyncOperation<\/code>, and the related progress types. It doesn&#8217;t define anything about <code>void<\/code>.<\/p>\n<p>From this, we can infer from the code the customer didn&#8217;t share that the <code>co_await<\/code> is happening in a function declared as returning <code>void<\/code>. The compiler is complaining that nobody has taught it how to <code>co_await<\/code> in a function that returns <code>void<\/code>.<\/p>\n<p>The C++\/WinRT return types that support being used as coroutines are <code>fire_and_forget<\/code> and the various <code>IAsyncXxx<\/code> types, so choose one of those. You probably want <code>fire_and_forget<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you even writing a coroutine?<\/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-107958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Are you even writing a coroutine?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107958","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=107958"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107958\/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=107958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}