July 20th, 2026
likemind blown2 reactions

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

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”?

More in part 2.

Topics

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Sort by :
  • IS4

    I hope explanation of what an “agile delegate” is is coming in part 2!

    • LB

      I think “agile” in COM means it’s not bound to a specific thread, so I’d guess “agile delegate” would be one which can be invoked from different threads.