Making an agile version of a Windows Runtime delegate in C++/WinRT, part 2
cppdashboard.dev/r/2026/08/making-an-agile-version-of-a-windows-runtime-delegate-in-cpp-9This article discusses how to create an agile version of a Windows Runtime delegate in C++/WinRT, including handling cases where the delegate is already agile. It provides code examples and insights into the implementation.
From the article
Last time, we had a straightforward function that makes an agile version of a Windows Runtime delegate . But there’s more to it.
In many cases, the delegate is already agile, so there’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 IAgileObject , which all agile objects possess.
template<typename Delegate> Delegate make_agile_delegate(Delegate const& d) { if (d.try_as<::IAgileObject>()) { return d; } return [agile = winrt::agile_ref(d)](auto&&...args) { return agile.get()(std::forward<decltype(args)>(args)...); }; } If the delegate declares itself as agile, then the delegate can be its own agile wrapper.
Share this resource