我包含哪个 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++ 的初学者,我担心以下问题:
- 我能否包含任何 header 文件并确保
size_t
的行为方式与我包含的哪个 header 文件无关?
- 是否有任何我需要注意的惊喜,例如包含一个 header 文件会令人惊讶 side-effects 而包含另一个 header 文件则不会?
- 是否有关于此的编码约定或流行约定或实践导致大多数人包括特定 header 文件以获得
size_t
的定义?
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
.
的标准定义
根据 http://en.cppreference.com/w/cpp/types/size_t ,类型 size_t
在许多 header 文件中定义:cstddef、cstdio、cstdlib 等
在编写自己的代码时,我应该包含哪个 header 文件才能使用 size_t
?
这似乎是一个微不足道的问题,但作为 C++ 的初学者,我担心以下问题:
- 我能否包含任何 header 文件并确保
size_t
的行为方式与我包含的哪个 header 文件无关? - 是否有任何我需要注意的惊喜,例如包含一个 header 文件会令人惊讶 side-effects 而包含另一个 header 文件则不会?
- 是否有关于此的编码约定或流行约定或实践导致大多数人包括特定 header 文件以获得
size_t
的定义?
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
.