size_t 包含哪个标头
根据 cpprefernece size_t
在各种标头中定义,即 cstddef 、 cstdio、 cstdlib、 cstring、 ctime、 cuchar 和 (C++17 起) cwchar。
我的问题是为什么没有一个 size_t
的定义?如果我只想要 size_t
,我应该包含哪个标头?
According to cpprefernece size_t
is defined in various headers, namely
cstddef , cstdio, cstdlib, cstring, ctime, cuchar and (since C++17) cwchar.
My question is why is there not a single definition of size_t
? Which header do I include, if I just want size_t
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::size_t
有一个定义。它只是在多个标头中定义,因为这些标头本身使用该定义。指定用来定义它的任何一个。
我的选择通常是
,因为它非常小,并且具有我可能需要的其他定义,例如std::byte
。There is a single definition for
std::size_t
. It's just defined in multiple headers since those headers themselves use that definition.Any of the ones that are specified to define it.
My choice is typically,
<cstddef>
because it is very small and has other definitions that I'm likely to need such asstd::byte
.一般来说,任何标准库标头都可以将任何其他标头的声明引入到 std 命名空间中,但这些声明将始终引用相同的实体,并且不会出现任何单定义规则违规或任何其他情况像那样。 cppreference 上列出的标头只是保证它们引入
std::size_t
的标头。
可能是最小的标头,并且也可以在独立环境中使用。但请注意,所列出的标头都不能保证将
size_t
引入全局命名空间范围。如果您需要这样做,则需要包含标头的 C 版本之一,例如#include
。Generally any standard library header may introduce any of the other header's declarations into the
std
namespace, but the declarations will always refer to the same entity and there won't be any one-definition-rule violation or anything like that. The headers listed on cppreference are just the ones for which a guarantee is made that they introducestd::size_t
.<cstddef>
is probably the most minimal out of the headers and will also be available in a freestanding environment.Note however, that none of the listed headers is guaranteed to introduce
size_t
into the global namespace scope. If you require this, you need to include one of the C versions of the headers, e.g.#include<stddef.h>
.