Should I include stddef.h or cstddef for size_t

前端 未结 5 1617
暗喜
暗喜 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:28

    is officially a deprecated part of C++ (along with the rest of Annex D of the C++ standard). All of these are (non-deprecated) parts of Standard C, so even though they're deprecated in C++, they're virtually certain to remain available almost indefinitely.

    A lot of features that aren't deprecated will almost certain disappear first -- export is already gone from the current draft of C++0x, and if I had to guess, I'd say exception specifications were a lot more likely to go than Annex D. When/if these headers do become truly obsolete, it'll probably be from a mature version of David Vandervoorde's modules proposal, which could easily render all headers obsolete.

    At the same time, a fair number of compilers (especially older ones) don't implement the headers exactly the way the standard prescribes. If you want/need to write code that works with them, you gain quite a bit by using the <*.h> headers instead of the headers.

    Ultimately, I think the headers were a solution in search of a problem. The C standard requires that these headers only define the names that are required -- no others at all except names that are reserved, such as with a leading underscore followed by another underscore or a capital letter. The reserved names (and a few more) are reserved in C++ as well, so they can't collide with anything in portable code in any case. As such, all the headers buy you is the ability to define a name in the global namespace that collides with an existing name in the C standard library. That is such a spectacularly awful idea that it's not even worth considering doing, so from a practical viewpoint you've gained nothing.

    Edit: Even that useless capability worked with few enough real compilers that the current drafts of the up-combing C++0x give permission for the headers to pollute the global namespace anyway, so even the theoretical advantage is gone.

提交回复
热议问题