本文介绍了我应该为`size_t'包含哪个标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 size_t 在多个标题中定义,即

 < cstddef> 
< cstdio>
< cstring>
< ctime>

而且,由于C ++ 11,还在

 < cstdlib> 
< cwchar>

首先我不知道为什么会出现这种情况。这不是与原则相矛盾吗?但是,我的问题是:



我应该使用以下哪个标头来使用 size_t

解决方案

假设我想最小化我导入的函数和类型使用 cstddef ,因为它不声明任何函数,只声明6种类型。



请注意, csttddef 只保证定义 std :: size_t ,即在命名空间中定义 size_t ,尽管 也可以在全局命名空间中提供此名称(有效地, size_t )。

相反, stddef.h (这也是C中可用的头)保证定义 size_t 在全局命名空间中, 也可提供 std :: size_t


According to cppreference.com size_t is defined in several headers, namely

<cstddef>
<cstdio>
<cstring>
<ctime>

And, since C++11, also in

<cstdlib>
<cwchar>

First of all I wonder why this is the case. Isn't this in contradiction to the DRY principle? However, my question is:

Which one of the above headers should I include to use size_t? Does it matter at all?

解决方案

Assuming I wanted to minimize the functions and types I was importing I'd go with cstddef as it doesn't declare any functions and only declares 6 types. The others focus on particular domains (strings, time, IO) that may not matter to you.

Note that csttddef only guarantees to define std::size_t, that is, defining size_t in namespace std, although it may provide this name also in the global namespace (effectively, plain size_t).

In contrast, stddef.h (which is also a header available in C) guarantees to define size_t in the global namespace, and may also provide std::size_t.

这篇关于我应该为`size_t'包含哪个标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:07