XCode上的size_t [英] size_t on XCode
问题描述
在XCode上,我似乎需要使用std :: size_t而不是Visual C ++上的size_t.但这是一个痛苦,因为我真的不想必须将代码中的每个size_t
更改为std::size_t
...在我的Windows代码size_t
中可以正常工作而无需包含任何其他文件.
It seems on XCode I need to use std::size_t instead of just size_t on Visual C++. But this is a pain as I don't really want to have to #include <cstddef>
and change every size_t
to std::size_t
in my code... in my Windows code size_t
just works without including any additional files.
是否有办法使现有代码在XCode中工作(也许通过.pch文件?),或者在这方面GCC/MSVC ++根本不同,因此我的代码需要使用std::size_t
才能跨平台?
Is there a way to make my existing code work in XCode, (maybe through the .pch file?) or are GCC/MSVC++ fundamentally different in this regard and my code needs to use std::size_t
in order to be cross-platform?
推荐答案
根据C ++ 03标准17.4.1.2.4:
According to the C++03 standard, 17.4.1.2.4:
除第18至27条另有规定外,每个标头cname的内容应与相应标头名称.h的内容相同,如ISO/IEC 9899:1990编程语言C(第7条)所规定,或ISO/IEC:1990年编程语言-C修订1:C完整性(第7条),视情况而定,好像包含在内.但是,在C ++标准库中,声明和定义(在C中定义为宏的名称除外)都在名称空间std的名称空间范围(3.3.5)之内.
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之内.
In other words, by choosing to use <cstddef>
instead of <stddef.h>
, you're specifically asking for the type size_t to be within the namespace std.
所以,这是选择:
-
请改用
<stddef.h>
,因此size_t
在顶级名称空间中,如Jesse Good所建议.
Use
<stddef.h>
instead, sosize_t
is in the top-level namespace, as suggested by Jesse Good.
使用<cstddef>
并使用std::size_t
.
使用<cstddef>
并使用using声明将size_t
拉入顶级名称空间.
Use <cstddef>
and use a using declaration to pull size_t
into the top-level namespace, as suggested by cnicutar.
当然,您可以依靠这样的事实:一个编译器/库/平台的特定版本可以使您摆脱它,或者为每个平台编写不同的代码,或者使用autoconf将整个内容包装起来,或者编写代码生成器或基于sed的预处理器,等等…
Of course you could rely on the fact that one particular version of one compiler/library/platform lets you get away with it, or write different code for each platform, or wrap the whole thing up with autoconf, or write a code generator or sed-based preprocessor, or whatever… but why?
这篇关于XCode上的size_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!