c - 为什么使用在 32 位和 64 位之间切换的通用 Int 比显式使用 Int32 和 Int64 更好

标签 c swift

<分区>

在新的 Apple Swift 文档中,它说:

Int

In most cases, you don’t need to pick a specific size of integer to use in your code. Swift provides an additional integer type, Int, which has the same size as the current platform’s native word size:

On a 32-bit platform, Int is the same size as Int32. On a 64-bit platform, Int is the same size as Int64. Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.

我可以理解,当使用用“Int”定义的 API 时,您应该使用它们。

但对于我自己的代码,我一直严格要求在 C 语言中使用 stdint header 使用适当的位大小类型。我的想法是我会尝试减少歧义。然而,Apple 的人非常聪明,我想知道我是否遗漏了什么,因为这不是他们推荐的。

最佳答案

这个主题没有得到广泛认同。

使用通用类型的优点是可移植性。同一段代码的编译和运行与平台的字长无关。在某些情况下,它也可能更快。

使用特定类型的优点是精度。没有歧义的余地,并且类型的确切功能是提前知道的。

没有绝对正确的答案。如果您出于任何和所有目的坚持问题的任何一方,您迟早会发现自己破例了。

关于c - 为什么使用在 32 位和 64 位之间切换的通用 Int 比显式使用 Int32 和 Int64 更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24092847/

相关文章:

C (gcc) 警告 : initialization from incompatible pointer type when calling pthread_cleanup_push()

c - 执行完所有程序后在程序末尾出现段错误?

c - c中的库函数返回值到哪里?

c - 获取用户输入,然后执行 execvp

ios - 写入上一个 Realm 对象而不是下一个

ios - 如何将 URL 读入 AVURLAsset

ios - Swift 托管对象上下文保存失败

C - calloc() 诉 malloc()

swift - 如何将两个不同的 View Controller 设置为同一个选项卡栏项目?

swift - 在全局方法中创建 UIAlertController