[MS] Making an agile version of a Windows Runtime delegate in C++/WinRT, part 1 - devamazonaws.blogspot.com

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.


Post Updated on July 20, 2026 at 03:00PM
Thanks for reading
from devamazonaws.blogspot.com

Comments

Popular posts from this blog

[MS] Pulling a single item from a C++ parameter pack by its index, remarks - devamazonaws.blogspot.com

[MS] Boosting Azure DevOps Security with GHAS Code Scanning - devamazonaws.blogspot.com

[MS] How can I check that all the changes in a git branch have been cherry-picked or rebased into its upstream branch? - devamazonaws.blogspot.com