{"id":112550,"date":"2026-07-21T07:00:00","date_gmt":"2026-07-21T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112550"},"modified":"2026-07-21T10:09:13","modified_gmt":"2026-07-21T17:09:13","slug":"20260721-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260721-00\/?p=112550\/","title":{"rendered":"Making an agile version of a Windows Runtime delegate in C++\/WinRT, part 2"},"content":{"rendered":"<p>Last time, we had <a title=\"Making an agile version of a Windows Runtime delegate in C++\/WinRT, part 1\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260720-00\/?p=112545\"> a straightforward function that makes an agile version of a Windows Runtime delegate<\/a>. But there&#8217;s more to it.<\/p>\n<p>In many cases, the delegate is already agile, so there&#8217;s no need to make an agile wrapper for something that is already agile. We can detect this case by looking for the marker interface <code>IAgile\u00adObject<\/code>, which all agile objects possess.<\/p>\n<pre>template&lt;typename Delegate&gt;\r\nDelegate make_agile_delegate(Delegate const&amp; d)\r\n{\r\n    <span style=\"border: solid 1px currentcolor; border-bottom: none;\">if (d.try_as&lt;::IAgileObject&gt;()) {<\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">    return d;                    <\/span>\r\n    <span style=\"border: solid 1px currentcolor; border-top: none;\">}                                <\/span>\r\n\r\n    return [agile = winrt::agile_ref(d)](auto&amp;&amp;...args) {\r\n        return agile.get()(std::forward&lt;decltype(args)&gt;(args)...);\r\n    };\r\n}\r\n<\/pre>\n<p>If the delegate declares itself as agile, then the delegate can be its own agile wrapper.<\/p>\n<p>Wait, there another case that we missed. We&#8217;ll look at that next time.<\/p>\n<p><b>Bonus chatter<\/b>: You might think that we could try to get copy elision in the case of an agile delegate:<\/p>\n<pre>template&lt;typename Delegate&gt;\r\nDelegate make_agile_delegate(<span style=\"border: solid 1px currentcolor;\">Delegate<\/span> d)\r\n{\r\n    if (d.try_as&lt;::IAgileObject&gt;()) {\r\n        return d; <span style=\"border: solid 1px currentcolor;\">\/\/ copy elision?<\/span>\r\n    }\r\n\r\n    return [agile = winrt::agile_ref(d)](auto&amp;&amp;...args) {\r\n        return agile.get()(std::forward&lt;decltype(args)&gt;(args)...);\r\n    };\r\n}\r\n<\/pre>\n<p>Unfortunately, this doesn&#8217;t work because function parameters are not eligible for copy elision.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short-circuiting the easiest case.<\/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-112550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Short-circuiting the easiest case.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112550","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=112550"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112550\/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=112550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}