Difference between typeof, __typeof and __typeof__ in Objective-C

后端 未结 3 1787
旧巷少年郎
旧巷少年郎 2020-12-07 11:02

In Objective-C I often use __typeof__(obj) when dealing with blocks etc. Why not __typeof(obj) or typeof(obj).

When to use whi

3条回答
  •  执念已碎
    2020-12-07 11:37

    As others have mentioned, typeof() is an extension of C that has various support in respective compilers.
    If you happen to be writing Objective-C for iOS or Mac apps, chances are good that you will be compiling your app with the Clang compiler.

    Clang does support the use of typeof(), but technically it's for when your C Language Dialect is set to be a gnu* type. However __typeof__() is supported in both c* and gnu* language dialects - as detailed in the Clang documentation.

    Now if you're writing your code with Xcode, the default setting for the C language dialect appears to be GNU99 and the option of allowing 'asm' 'inline' 'typeof' is set to Yes, so using typeof() won't bring you any problems.

    Xcode project settings

    If you want to be (arguably) safer when using the Clang compiler, use __typeof__(). This way you won't be affected if the C Language Dialect being used for compilation changes or if someone decides to turn off the allowance of 'typeof'.

提交回复
热议问题