So I know any header from the C Compatability Headers:
Places in the global namespace each name that the corresponding cxxx header would have placed in the
std
namespace
I also know that these C headers were deprecated as of c++17, in favor of their compatibility "cxxx" counterparts.
Now, I believe that size_t
is defined exclusively by the Standard Defines Header. So I presume this technically means that the definition of size_t
in the global namespace has been deprecated?
I've been using it for years as just size_t
and I'd just like a confirmation before I move to using std::size_t
.
I presume this technically means that the definition of size_t in the global namespace has been deprecated?
The Standard only mandates that std::size_t
must be defined1 by <cstddef>
, it does not disallow an implementation to define ::size_t
2, but if the implementation does, the two definitions must match3.
As a conclusion, you should use std::size_t
and should neither rely on ::size_t
to be defined nor define it.
The following are UB:
// DON'T
using size_t = std::size_t; // UB
using size_t = decltype(sizeof 1); // UB
1)[cstddef.syn]
namespace std { using ptrdiff_t = see below; using size_t = see below; using max_align_t = see below; using nullptr_t = decltype(nullptr);
[...]
The contents and meaning of the header<cstddef>
are the same as the C standard library header<stddef.h>
, except that it does not declare the typewchar_t
, that it also declares the typebyte
and its associated operations ([support.types.byteops]
), and as noted in[support.types.nullptr]
and[support.types.layout]
.
2)[extern.types]/1
For each type
T
from the C standard library (These types are [...]size_t
,[...].), the types ::T
andstd::T
are reserved to the implementation[.]
3)[extern.types]/1
[...] when defined,
::T
shall be identical tostd::T
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With