Article

Making an agile version of a Windows Runtime delegate in C++/WinRT, part 2

The Old New Thing ·The Old New Thing ·Published 2026-07-21 ·5 min read

This 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 IAgile­Object , 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


Discovered 2026-08-02 Source The Old New Thing Archive 2026-08 →