In some code I’ve inherited, I see frequent use of size_t with the std namespace qualifier. For example:
std::size_t n = sizeof( long );
It compiles and runs fine, of course. But it seems like bad practice to me (perhaps carried over from C?).
Isn’t it true that size_t is built into C++ and therefore in the global namespace? Is a header file include needed to use size_t in C++?
Another way to ask this question is, would the following program (with no includes) be expected to compile on all C++ compilers?
size_t foo() { return sizeof( long ); }
There seems to be confusion among the stackoverflow crowd concerning this
::size_tis defined in the backward compatibility headerstddef.h. It’s been part ofANSI/ISO CandISO C++since their very beginning. Every C++ implementation has to ship withstddef.h(compatibility) andcstddefwhere only the latter definesstd::size_tand not necessarily::size_t. See Annex D of the C++ Standard.