[cfe-dev] Type traits to identify pointers to ObjC types?

David Chisnall csdavec at swan.ac.uk
Sun Dec 18 10:12:14 PST 2011


Is this actually required to accomplish what you want?  Any Objective-C type can be implicitly cast to id, so if you have a template specialisation that uses id then it will be selected for any Objective-C type.  You can, for example, implement something like this:

template<typename T>
bool is_objc_type(T x) { return false; }

template<>
bool is_objc_type(id x) { return true; }

Which will return true when called with an Objective-C type as an argument and false when called with any other type.  

The reason ARC will break with the Qt containers (as you have explained them, and without looking at the code) is that casting Objective-C types to void* is not allowed even with an explicit cast.  It requires a bridged cast to ensure that ownership is preserved.  

This works with the STL templates because Objective-C types in ARC mode are implicitly non-POD types and have custom behaviour for assignment and so the compiler will instantiate the templates and insert the objc_retain() and friends calls as required.  

David

On 18 Dec 2011, at 17:17, Nicola Gigante wrote:

> Hello.
> 
> Does clang support any built-in type trait to identify if a given T* is
> a pointer to an ObjC class?
> 
> This could be useful to specialize some templates in library code
> that is meant to be useful in ObjectiveC++ mode.
> 
> For example, some non-STL generic containers like Qt ones (especially QList),
> use a trick to reduce code size: they use a non-template internal implementation
> that handles void* elements, and then cast to the specific types in a lightweight
> templated wrapper. For this reason, i believe ARC breaks with Qt containers,
> while it works like a charm with the STL.
> Should they want to make Qt containers work with ARC, they'd need to know
> when they're dealing with ObjC objects pointers and someway specialize the 
> implementation.
> 
> Does such a trait exist? If not (as I suppose), what do you think about adding it?
> 
> Thanks,
> Nicola
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the cfe-dev mailing list