类型、__typeof和__typeof__ (Objective-c)的区别

时间:2022-03-17 21:29:54

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

在objective-c中,我经常使用__typeof__(obj)来处理块等等。为什么不使用__typeof(obj)或typeof(obj)呢?

When to use which?

什么时候使用哪个?

3 个解决方案

#1


47  

__typeof__() and __typeof() are compiler-specific extensions to the C language, because standard C does not include such an operator. Standard C requires compilers to prefix language extensions with a double-underscore (which is also why you should never do so for your own functions, variables, etc.)

__typeof__()和__typeof()是C语言的特定于编译器的扩展,因为标准C不包含这样的操作符。标准C要求编译器在语言扩展前面加上双下划线(这也是为什么您永远不应该对自己的函数、变量等这样做)。

typeof() is exactly the same, but throws the underscores out the window with the understanding that every modern compiler supports it. (Actually, now that I think about it, Visual C++ might not. It does support decltype() though, which generally provides the same behaviour as typeof().)

typeof()是完全相同的,但是将下划线抛出窗口,并理解每个现代编译器都支持它。(实际上,现在我想起来了,Visual c++可能不会。不过,它确实支持decltype(),它通常提供与typeof()相同的行为。

All three mean the same thing, but none are standard C so a conforming compiler may choose to make any mean something different.

这三种意思都是一样的,但是没有一种是标准的C,所以符合标准的编译器可能会选择使任何意义有所不同。

#2


25  

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.

正如其他人提到的,typeof()是C的扩展,它在各自的编译器中有各种支持。如果你正在为iOS或Mac应用编写Objective-C,那么你很有可能会使用Clang编译器来编译你的应用。

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.

Clang确实支持typeof()的使用,但从技术上讲,它适用于将C语言方言设置为gnu*类型的情况。然而,__typeof__()在c*和gnu*语言的方言中都得到了支持——在Clang文档中有详细的说明。

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编写代码,那么C语言方言的默认设置似乎是GNU99,允许“asm”“inline”“typeof”的选项设置为Yes,因此使用typeof()不会带来任何问题。

类型、__typeof和__typeof__ (Objective-c)的区别

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

如果您希望(可以说)在使用Clang编译器时更安全,请使用__typeof__()。这样,如果用于编译的C语言方言发生变化,或者某人决定关闭“typeof”的允许范围,您就不会受到影响。

#3


4  

Hope this will be helpfull:

希望这能有所帮助:

-ansi and the various -std options disable certain keywords. This causes trouble when you want to use GNU C extensions, or a general-purpose header file that should be usable by all programs, including ISO C programs. The keywords asm, typeof and inline are not available in programs compiled with -ansi or -std (although inline can be used in a program compiled with -std=c99 or -std=c11). The ISO C99 keyword restrict is only available when -std=gnu99 (which will eventually be the default) or -std=c99 (or the equivalent -std=iso9899:1999), or an option for a later standard version, is used.

-ansi和各种-std选项禁用某些关键字。当您希望使用GNU C扩展或通用的头文件时,这会造成麻烦,这些头文件应该可以被所有程序使用,包括ISO C程序。asm、typeof和inline在使用-ansi或-std编译的程序中不可用(尽管inline可用于使用-std=c99或-std=c11编译的程序中)。ISO C99关键字limit仅在使用-std=gnu99(最终将是默认值)或-std= C99(或等效的-std=iso9899:1999)或后续标准版本的选项时可用。

The way to solve these problems is to put ‘__’ at the beginning and end of each problematical keyword. For example, use __asm__ instead of asm, and __inline__ instead of inline.

解决这些问题的方法是在每个问题关键字的开头和结尾写上__。例如,使用__asm__代替asm,使用__inline__代替inline - __。

http://gcc.gnu.org/onlinedocs/gcc/Alternate-Keywords.html#Alternate-Keywords

http://gcc.gnu.org/onlinedocs/gcc/Alternate-Keywords.html Alternate-Keywords

#1


47  

__typeof__() and __typeof() are compiler-specific extensions to the C language, because standard C does not include such an operator. Standard C requires compilers to prefix language extensions with a double-underscore (which is also why you should never do so for your own functions, variables, etc.)

__typeof__()和__typeof()是C语言的特定于编译器的扩展,因为标准C不包含这样的操作符。标准C要求编译器在语言扩展前面加上双下划线(这也是为什么您永远不应该对自己的函数、变量等这样做)。

typeof() is exactly the same, but throws the underscores out the window with the understanding that every modern compiler supports it. (Actually, now that I think about it, Visual C++ might not. It does support decltype() though, which generally provides the same behaviour as typeof().)

typeof()是完全相同的,但是将下划线抛出窗口,并理解每个现代编译器都支持它。(实际上,现在我想起来了,Visual c++可能不会。不过,它确实支持decltype(),它通常提供与typeof()相同的行为。

All three mean the same thing, but none are standard C so a conforming compiler may choose to make any mean something different.

这三种意思都是一样的,但是没有一种是标准的C,所以符合标准的编译器可能会选择使任何意义有所不同。

#2


25  

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.

正如其他人提到的,typeof()是C的扩展,它在各自的编译器中有各种支持。如果你正在为iOS或Mac应用编写Objective-C,那么你很有可能会使用Clang编译器来编译你的应用。

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.

Clang确实支持typeof()的使用,但从技术上讲,它适用于将C语言方言设置为gnu*类型的情况。然而,__typeof__()在c*和gnu*语言的方言中都得到了支持——在Clang文档中有详细的说明。

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编写代码,那么C语言方言的默认设置似乎是GNU99,允许“asm”“inline”“typeof”的选项设置为Yes,因此使用typeof()不会带来任何问题。

类型、__typeof和__typeof__ (Objective-c)的区别

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

如果您希望(可以说)在使用Clang编译器时更安全,请使用__typeof__()。这样,如果用于编译的C语言方言发生变化,或者某人决定关闭“typeof”的允许范围,您就不会受到影响。

#3


4  

Hope this will be helpfull:

希望这能有所帮助:

-ansi and the various -std options disable certain keywords. This causes trouble when you want to use GNU C extensions, or a general-purpose header file that should be usable by all programs, including ISO C programs. The keywords asm, typeof and inline are not available in programs compiled with -ansi or -std (although inline can be used in a program compiled with -std=c99 or -std=c11). The ISO C99 keyword restrict is only available when -std=gnu99 (which will eventually be the default) or -std=c99 (or the equivalent -std=iso9899:1999), or an option for a later standard version, is used.

-ansi和各种-std选项禁用某些关键字。当您希望使用GNU C扩展或通用的头文件时,这会造成麻烦,这些头文件应该可以被所有程序使用,包括ISO C程序。asm、typeof和inline在使用-ansi或-std编译的程序中不可用(尽管inline可用于使用-std=c99或-std=c11编译的程序中)。ISO C99关键字limit仅在使用-std=gnu99(最终将是默认值)或-std= C99(或等效的-std=iso9899:1999)或后续标准版本的选项时可用。

The way to solve these problems is to put ‘__’ at the beginning and end of each problematical keyword. For example, use __asm__ instead of asm, and __inline__ instead of inline.

解决这些问题的方法是在每个问题关键字的开头和结尾写上__。例如,使用__asm__代替asm,使用__inline__代替inline - __。

http://gcc.gnu.org/onlinedocs/gcc/Alternate-Keywords.html#Alternate-Keywords

http://gcc.gnu.org/onlinedocs/gcc/Alternate-Keywords.html Alternate-Keywords