Should I include stddef.h or cstddef for size_t

前端 未结 5 1669
暗喜
暗喜 2020-12-03 06:39

When I want to use size_t in C++, should I include or ? I have heard several people saying that

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 07:17

    I prefer #include .

    Some of the names in the C headers are allowed to be macros, but the set differs from the C rules. In C, EXIT_FAILURE, isdigit(), getc() a.o. are macros. Do you know which ones are macros in C++?

    Secondly, only a couple standard C headers are required to have the header, Posix headers are not. Do you know which headers are standard and which ones are provided by your compiler only?

    Thirdly, when using headers from a third-party C library, you will end up with #include , and I prefer not to mix and .

    Fourthly, the current draft for the new C++ standard says that is allowed to dump the symbols into the global namespace (because apparently many compilers already do so nowadays), so using #include is not a guarantee that the global namespace will be unpolluted in the future. So I would advice that when writing portable code, you should assume the global namespace will be affected (even though it is not allowed now). As only a few experts seem to know this (see the discussion in the comments here), it is better to use as even a beginning C++ programmer will understand that it pollutes the global namespace.

提交回复
热议问题