似乎在 XCode 上我需要使用 std::size_t 而不是在 Visual C++ 上只使用 size_t。但这很痛苦,因为我真的不想#include <cstddef>
并更改每个 size_t
至 std::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 内。
所以,这里是选择:
使用
<stddef.h>
相反,所以size_t
正如 Jesse Good 所建议的那样,位于顶级命名空间中。使用
<cstddef>
并使用std::size_t
.使用
<cstddef>
并使用 using 声明来拉取size_t
按照 cnicutar 的建议,进入顶级命名空间。
当然,你可以相信一个编译器/库/平台的特定版本可以让你摆脱它,或者为每个平台编写不同的代码,或者用 autoconf 包装整个事情,或者写一个代码生成器或基于 sed 的预处理器,或其他……但为什么呢?
关于c++ - XCode 上的 size_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10823878/