所以我知道 C Compatability Headers 中的任何标题:
Places in the global namespace each name that the corresponding cxxx header would have placed in the
std
namespace
我还知道这些 C 头文件自 c++17 起已被弃用。 , 支持它们的兼容性“cxxx”对应物。
现在,我相信 size_t
完全由 Standard Defines Header 定义.所以我认为这在技术上意味着全局命名空间中 size_t
的定义已被弃用?
多年来我一直将它用作 size_t
,在我开始使用 std::size_t
之前,我希望得到确认。
最佳答案
I presume this technically means that the definition of size_t in the global namespace has been deprecated?
是的……但是。
标准仅规定 std::size_t
必须由 <cstddef>
定义1 , 它不允许实现定义 ::size_t
2,但如果实现了,则这两个定义必须匹配3。
作为结论,您应该使用 std::size_t
也不应该依赖 ::size_t
未定义也未定义。
以下是UB:
// DON'T
using size_t = std::size_t; // UB
using size_t = decltype(sizeof 1); // UB
namespace std { using ptrdiff_t = see below; using size_t = see below; using max_align_t = see below; using nullptr_t = decltype(nullptr);
[...]
The contents and meaning of the header<cstddef>
are the same as the C standard library header<stddef.h>
, except that it does not declare the typewchar_t
, that it also declares the typebyte
and its associated operations ([support.types.byteops]
), and as noted in[support.types.nullptr]
and[support.types.layout]
.
For each type
T
from the C standard library (These types are [...]size_t
,[...].), the types ::T
andstd::T
are reserved to the implementation[.]
[...] when defined,
::T
shall be identical tostd::T
.
关于c++ - size_t 在哪里定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51500719/