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 );
}
size_t is not built into C++. And it is not defined by default. This one doesn't compile with GCC:
That said, size_t is part of POSIX and if you use only basic things like
<cstdlib>
, you will likely end up having it defined.You could argue that std::size_t is the C++ equivalent of size_t. As Brian pointed out, std:: is used as namespace to avoid setting global variables which don't fit everybody. It's just like std::string, which could also have been defined in the root namespace.
Sometimes other libraries will define their own size_t. For example boost. std::size_t specifies that you definitely want the c++ standard one.
size_t is a c++ standard type and it is defined within the namespace std.
I think the clarifications are clear enough. The
std::size_t
makes good sense in C++ and::size_t
make (at least) good sense in C.However a question remain. Namely whether it is safe to assume that
::size_t
andstd::size_t
are compatible?From a pure typesafe perspective they are not necessarily identical unless it is defined somewhere that they must be identical.
I think many are using something a la:
So in the header you defintely use the
::size_t
while in the source file you'll usestd::size_t
. So they must be compatible, right? Otherwise you'll get a compiler error./Michael S.
There seems to be confusion among the stackoverflow crowd concerning this
::size_t
is defined in the backward compatibility headerstddef.h
. It's been part ofANSI/ISO C
andISO C++
since their very beginning. Every C++ implementation has to ship withstddef.h
(compatibility) andcstddef
where only the latter definesstd::size_t
and not necessarily::size_t
. See Annex D of the C++ Standard.You can get
size_t
in the global namespace by including, for example,<stddef.h>
instead of<cstddef>
. I can't see any obvious benefit, and the feature is deprecated.The GNU compiler headers contain something like
Then stddef.h constains something like
And finally the cstddef file contains something like
I think that should make it clear. As long as you include <cstddef> you can use either size_t or std::size_t because size_t was typedefed outside the std namespace and was then included. Effectively you have