{"id":104778,"date":"2021-02-01T07:00:00","date_gmt":"2021-02-01T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104778"},"modified":"2021-02-02T06:35:46","modified_gmt":"2021-02-02T14:35:46","slug":"2021021-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210201-00\/?p=104778","title":{"rendered":"Why am I being told my fire_and_forget coroutine is not returning a value?"},"content":{"rendered":"<p>You&#8217;re writing a C++\/WinRT coroutine that nominally returns a <code>fire_<wbr \/>and_<wbr \/>forget<\/code>, meaning that the coroutine runs without notifying anybody when it is finished.<\/p>\n<pre>fire_and_forget DoSomething()\r\n{ \r\n    DoFirstThing();\r\n    DoSecondThing();\r\n}\r\n<\/pre>\n<p>And when you compile this, you get<\/p>\n<pre style=\"white-space: pre-wrap;\">error C4716: 'DoSomething': must return a value\r\n<\/pre>\n<p>A <code>fire_and_forget<\/code> coroutine doesn&#8217;t have a return value, since there is nobody awaiting it. So what&#8217;s going on here?<\/p>\n<p>What&#8217;s going on is that the body of your <code>fire_and_forget<\/code> coroutine doesn&#8217;t contain any <code>co_await<\/code> or <code>co_return<\/code> statements. A function must contain at least one <code>co_await<\/code> or <code>co_return<\/code>\u00a0statement in order to be considered a coroutine. Since the function did neither of those things, the C++ language treats it not as a coroutine but a regular function.<\/p>\n<p>A regular function that needs to return a <code>fire_<wbr \/>and_<wbr \/>forget<\/code> object, and you didn&#8217;t do that.<\/p>\n<p>You have a few options for fixing this.<\/p>\n<p>One option is to add a <code>co_return;<\/code> statement at the end. Normally, falling off the end of a coroutine is equivalent to performing a <code>co_return;<\/code>, but in this case, you need to say <code>co_return<\/code> explicitly in order to make sure you have a coroutine at all!<\/p>\n<p>Another option is to change your function to return <code>void<\/code>. After all, it doesn&#8217;t contain any asynchronous operations, so the whole thing ran synchronously anyway. It never needed to be a coroutine in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fire-and-forget coroutines don&#8217;t return a value, do they?<\/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-104778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Fire-and-forget coroutines don&#8217;t return a value, do they?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104778","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=104778"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104778\/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=104778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}