我包含哪个 header(cstddef、cstdio、cstdlib 等)以获取 size_t 的定义是否重要?

Does it matter which header (cstddef, cstdio, cstdlib, etc.) I include to get the definition of size_t?

根据 http://en.cppreference.com/w/cpp/types/size_t ,类型 size_t 在许多 header 文件中定义:cstddef、cstdio、cstdlib 等

在编写自己的代码时,我应该包含哪个 header 文件才能使用 size_t

这似乎是一个微不足道的问题,但作为 C++ 的初学者,我担心以下问题:

Can I include any header file and be sure that size_t would behave in the same way regardless of which header file I included?

是的。

Are there any surprises I need to be aware of like including one header file would have surprising side-effects that including another header file would not have?

没有

Is there a coding convention or popular convention or practice regarding this that leads to most people including a specific header file to get the definition of size_t?

我个人更喜欢<cstddef>,但我不知道有什么约定。

Is there a coding convention or popular convention or practice regarding this that leads to most people including a specific header file to get the definition of size_t?

不,据我所知,没有或至少 none 如此受欢迎。就个人而言,在我只需要 std::size_t 的情况下,为了不从定义 std::size_t 的 headers 中拖出不必要的代码,我将自己的 size_t 定义为:

using size_t = decltype(sizeof(char));

注:以上也符合std::size_t.

的标准定义