Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obj-C++: template metafunction for recognizing Objective-C classes?

Using Objective-C++, can I write a C++ IsObjectiveCClass<T> template metafunction such that IsObjectiveCClass<T>::value is true if and only if T is an Objective-C class?

Exactly what are ObjC classes from the viewpoint of the C / C++ subset of the language? When used in a C / C++ context, MyClass* pointers seem to behave like ordinary C pointers; does that mean that MyClass is also a C type?

like image 227
imre Avatar asked Aug 12 '11 12:08

imre


1 Answers

Here is a simplistic solution that should work in most (if not all? Can anyone think of when this might fail?) cases (it uses clang 3.0 via xcode 4.2 - use typedefs instead of using aliases for earlier clang versions):

template<class T> struct IsObjectiveCClass 
{ 
  using yesT = char (&)[10];
  using noT = char (&)[1];
  static yesT choose(id);
  static noT choose(...);
  static T make();
  enum { value = sizeof(choose(make())) == sizeof(yesT) }; 

};
like image 147
Faisal Vali Avatar answered Sep 20 '22 13:09

Faisal Vali