c++ - XCode 上的 size_t

标签 c++ xcode visual-c++ gcc stl

似乎在 XCode 上我需要使用 std::size_t 而不是在 Visual C++ 上只使用 size_t。但这很痛苦,因为我真的不想#include <cstddef>并更改每个 size_tstd::size_t在我的代码中...在我的 Windows 代码中 size_t无需包含任何其他文件即可正常工作。

有没有办法让我现有的代码在 XCode 中工作(也许通过 .pch 文件?)或者 GCC/MSVC++ 在这方面有根本的不同,我的代码需要使用 std::size_t为了跨平台?

最佳答案

根据C++03标准,17.4.1.2.4:

Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate, as if by inclusion. In the C++ Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.

换句话说,通过选择使用 <cstddef>而不是 <stddef.h> ,您特别要求类型 size_t 位于命名空间 std 内。

所以,这里是选择:

  1. 使用 <stddef.h>相反,所以 size_t正如 Jesse Good 所建议的那样,位于顶级命名空间中。

  2. 使用 <cstddef>并使用 std::size_t .

  3. 使用 <cstddef>并使用 using 声明来拉取 size_t按照 cnicutar 的建议,进入顶级命名空间。

当然,你可以相信一个编译器/库/平台的特定版本可以让你摆脱它,或者为每个平台编写不同的代码,或者用 autoconf 包装整个事情,或者写一个代码生成器或基于 sed 的预处理器,或其他……但为什么呢?

关于c++ - XCode 上的 size_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10823878/

相关文章:

ios - 在 xcode 11 中打开项目时 UINavigationBar 重叠状态栏

ios - 配置文件 "**"的应用 ID 为 "**",与 bundle ID "**OneSignalNotificationServiceExtension"不匹配

c++ - 如何在boost中定义正则表达式?

c++ - 特定模板参数的模板断点

c++ - 在错误的对象上调用复制构造函数

c++ - 如何读取一个文件,反转文本的一部分并将反转的部分写入 C++ 上的另一个文件?

c++ - 无法将创建的 .so 文件链接到 main.cpp

c++ - 在多线程程序中使用 exprtk

c++ - 将可变参数模板参数与 lambda 参数匹配

C++ lambda 不会对按值捕获的成员调用析构函数