{"id":112545,"date":"2026-07-20T07:00:00","date_gmt":"2026-07-20T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112545"},"modified":"2026-07-21T09:19:35","modified_gmt":"2026-07-21T16:19:35","slug":"20260720-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260720-00\/?p=112545","title":{"rendered":"Making an agile version of a Windows Runtime delegate in C++\/WinRT, part 1"},"content":{"rendered":"<p>Suppose you have some C++\/WinRT code that receives a <a href=\"https:\/\/learn.microsoft.com\/en-us\/uwp\/midl-3\/intro#delegates\"> delegate<\/a> from an outside source, and you might invoke that delegate from a potentially different COM context. However, the original delegate may not be <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/apps\/develop\/threading\/winrt-objects-multithreaded\"> agile<\/a>. How can you make an agile version of that delegate?<\/p>\n<p>The easy way is to wrap the delegate in an <code>agile_<wbr \/>ref<\/code>, and then resolve the <code>agile_<wbr \/>ref<\/code> back to a delegate when you want to invoke it.<\/p>\n<pre>template&lt;typename Delegate&gt;\r\nDelegate make_agile_delegate(Delegate const&amp; d)\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>But if it were that easy, why would we call this article &#8220;part 1&#8221;?<\/p>\n<p>More in part 2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easy case is easy.<\/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-112545","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The easy case is easy.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112545","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=112545"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112545\/revisions"}],"predecessor-version":[{"id":112549,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112545\/revisions\/112549"}],"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=112545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}