microsoft / cppwinrt

C++/WinRT
MIT License
1.64k stars 236 forks source link

Can non_agile be used in a derived class? #1332

Closed amtopel closed 1 year ago

amtopel commented 1 year ago

I've been trying to create single-threaded classes in C++/WinRT using non_agile, and based on some anecdotal testing, it seems that non_agile works with classes that don't derive from a base class. But if a class does derive from a base class, then it doesn't work, and the class is agile.

Is that accurate?

And if so, is there any way to make a derived class non_agile?

Thanks for any assistance.

sylveon commented 1 year ago

Can you share some code?

kennykerr commented 1 year ago

non_agile just instructs the implements class template not to implement the IAgileObject and IMarshal interfaces by default but does not have any control over whether the composed base type does so.

kennykerr commented 1 year ago

We cannot change the QueryInterface implementation without wreaking havoc. 😊 The non_agile marker is not saying that the object is never to be agile but rather that the implements class template will not implement those two interfaces. This is typically used not to suppress agility but to allow someone to provide custom implementations of IMarshal.

kennykerr commented 1 year ago

You may want to stick with ATL. 😊

Note that agility and thread-safety are not the same thing and the composition support in C++/WinRT is only there to support Xaml where you really can't mess with the agility or concurrency of the base type.