Making an agile version of a Windows Runtime delegate in C++/WinRT, part 1
cppdashboard.dev/r/2026/08/making-an-agile-version-of-a-windows-runtime-delegate-in-cpp-10This article discusses how to create an agile version of a Windows Runtime delegate in C++/WinRT, particularly when dealing with different COM contexts. It introduces the use of agile_ref for wrapping delegates.
From the article
Suppose you have some C++/WinRT code that receives a delegate from an outside source, and you might invoke that delegate from a potentially different COM context. However, the original delegate may not be agile . How can you make an agile version of that delegate?
The easy way is to wrap the delegate in an agile_ ref , and then resolve the agile_ ref back to a delegate when you want to invoke it.
template<typename Delegate> Delegate make_agile_delegate(Delegate const& d) { return [agile = winrt::agile_ref(d)](auto&&...args) { return agile.get()(std::forward<decltype(args)>(args)...); }; } But if it were that easy, why would we call this article “part 1”?
Share this resource