{"id":104080,"date":"2020-08-13T07:00:00","date_gmt":"2020-08-13T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104080"},"modified":"2020-09-09T06:13:34","modified_gmt":"2020-09-09T13:13:34","slug":"20200813-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200813-00\/?p=104080","title":{"rendered":"Working around the requirement that Concurrency Runtime task results must be default-constructible and copyable"},"content":{"rendered":"<p><a title=\"Hidden constraints on the result type in Concurrency Runtime tasks\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200812-00\/?p=104075\"> Last time<\/a>, we shed light on the hidden constraints on the result type in Concurrency Runtime tasks: The result type must have a public default constructor, and it must be copyable.<\/p>\n<p>But what if your desired result type doesn&#8217;t satisfy these requirements?<\/p>\n<p>To work around the need for a public default constructor, you can wrap your result type inside something that does have a public default constructor, such as <code>std::optional<\/code>.<\/p>\n<pre>Concurrency::task&lt;std::optional&lt;T&gt;&gt;\r\n    t([]() { return T::make(); });\r\n<\/pre>\n<p>If you produce the result from a lambda, you can just return a <code>T<\/code>, and a <code>std::optional&lt;T&gt;<\/code> will be constructed from it. If you produce the result from a <code>task_<\/code><code>completion_<\/code><code>event<\/code>, you&#8217;ll have to use a <code>task_<\/code><code>completion_<\/code><code>event&lt;std::optional&lt;<\/code><code>T&gt;&gt;<\/code>. The result of the task will be an <code>optional&lt;T&gt;<\/code>, and you can use the dereference operator <code>*<\/code> to extract the value. (This assumes that the task always completes with a value, which I assume it does, because that&#8217;s what it did before you started down this path.)<\/p>\n<p>To work around the need for copyability, you can wrap the result in a <code>std::shared_ptr&lt;T&gt;<\/code>. That way, there is still only one <code>T<\/code> object, and all the continuations get the shared copy.<\/p>\n<p>And since <code>std::shared_ptr<\/code> has a public default constructor, if your result type falls into both categories (lacks a public default constructor, is not copyable), you can wrap it in a <code>std::shared_ptr&lt;T&gt;<\/code> and solve both problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Applying tricks to avoid the pitfalls.<\/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-104080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Applying tricks to avoid the pitfalls.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104080","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=104080"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104080\/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=104080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}