c++ - size_t 在哪里定义的?

标签 c++ types namespaces size-t global-namespace

所以我知道 C Compatability Headers 中的任何标题:

Places in the global namespace each name that the corresponding cxxx header would have placed in the std namespace

我还知道这些 C 头文件自 起已被弃用。 , 支持它们的兼容性“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

1) [cstddef.syn]

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 type wchar_­t, that it also declares the type byte and its associated operations ([support.types.byteops]), and as noted in [support.types.nullptr] and [support.types.layout].

2) [extern.types]/1

For each type T from the C standard library (These types are [...] size_­t,[...].), the types ​::​T and std​::​T are reserved to the implementation[.]

3) [extern.types]/1

[...] when defined, ​::​T shall be identical to std​::​T.

关于c++ - size_t 在哪里定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51500719/

相关文章:

xml - javax.xml.bind.UnmarshalException

namespaces - Laravel - Eloquent : Polymorphic relations with namespace

c++ - 在模板派生类中,为什么需要在成员函数中使用“this->”来限定基类成员的名字?

c++ - 在 64 位 VC++ 2010 上用于 boost 的预编译 .libs

C++ promise / future : Which to return from a function?

haskell - 如何检查 Haskell 中的数据类型?

c++ - 在 C++ 中复制对象

java - Java 缺少无符号原始类型是 Java 平台的特征还是 Java 语言的特征?

c# - .NET 整数与 Int16?

ruby-on-rails-3 - 如何创建自定义 rails 命名空间路由