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

Douglas Gregor dgregor at apple.com
Mon Dec 19 08:00:58 PST 2011


On Dec 18, 2011, at 1:53 PM, Nicola Gigante wrote:

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

This kind of thing works fine in C++98/03 as well. It's probably worth checking that T is a pointer type, so you don't treat something like

struct X {
	operator id() const;
};

as an Objective-C pointer type.

	- Doug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111219/2f1b984e/attachment.html>


More information about the cfe-dev mailing list