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

Nicola Gigante nicola.gigante at gmail.com
Sun Dec 18 13:53:16 PST 2011


Il giorno 18/dic/2011, alle ore 19:12, David Chisnall ha scritto:

> 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.  
> 

Well, that doesn't seam to work, because overloading resolution 
rules seems to always prefer the template version.

I had to write something like:

template<typename T>
inline typename std::enable_if<!std::is_convertible<T, id>::value, bool>::type
is_objc_type(T t) { return false; }

inline bool is_objc_type(id obj) { return true; }

But you got the point, yes, an intrinsic is not needed.
However, this is still possible only in C++11 mode with
the is_convertible type trait, or am I missing something?

> 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.  
> 

I know. That's why an hypotetical ARC-friendly Qt implementation (or any similar piece of code)
would need to specialize properly on ObjC types, to avoid those void* tricks.

> David

Thanks,
Nicola





More information about the cfe-dev mailing list