Where is size_t Defined? [duplicate]
Asked Answered
L

4

9

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 , 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.

Lubalubba answered 24/7, 2018 at 14:6 Comment(3)
Now, I believe that size_t is defined exclusively by the Standard Defines Header is not exactly correct, since many of the C compatibility library headers besides cstddef define it.Rohde
@François I'm not sure this is a duplicate. OP wants to know if ::size_t is deprecated.Sedgemoor
@Sedgemoor The duplicate does address that ::size_t is not required to be define. I would reason that it addresses the part about deprecation because the standard can't deprecate something it doesn't define.Harl
S
7

I presume this technically means that the definition of size_t in the global namespace has been deprecated?

Yes... but.

The Standard only mandates that std::size_t must be defined1 by <cstddef>, it does not disallow an implementation to define ::size_t2, 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 type wchar_­t, that it also declares the type byte 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 and std​::​T are reserved to the implementation[.]

3)[extern.types]/1

[...] when defined, ​::​T shall be identical to std​::​T.

Sedgemoor answered 24/7, 2018 at 14:10 Comment(0)
N
3

C-style header names like <stddef.h> are deprecated. However, C++-style headers like <cstddef> are allowed to declare their names in global namespace and then redeclare the same names in namespace std through using-declarations (http://eel.is/c++draft/organization#headers-4). This approach to declaring standard names is not deprecated. And many implementations do exactly that, for which reason it is not unusual to see name size_t accessible as a name from global namespace. But this is not guaranteed. So, in portable C++ code that includes <cstddef> you should use std::size_t and never rely on ::size_t's availability.

Nucleolar answered 24/7, 2018 at 14:49 Comment(0)
R
1

The standard says [expr.sizeof]:

The result of sizeof and sizeof... is a constant of type std​::​size_­t. [ Note: std​::​size_­t is defined in the standard header <cstddef> ([cstddef.syn], [support.types.layout]). — end note ]

Rotter answered 24/7, 2018 at 14:12 Comment(6)
You have a similar answer in the duplicate.Harl
@FrançoisAndrieux This answer is different because it does not add to std::.Rotter
Based on the above, I do not see why you cannot do: using size_t = decltype(sizeof 1); You can't do that (in global namespace). ::size_t is a reserved name, so defining it has undefined behaviour.Rohde
@user2079303 not anymore.Sedgemoor
@Sedgemoor since when? It's reserved here. Does the draft differ from the published standard? For each type T ... the types ​::​T and ... are reserved to the implementation ... These types are ... size_­t ...Rohde
@user2079303 Retracted that claim, it does not seem to be too popular.Rotter
R
0

Where is size_t Defined?

::size_t is guaranteed to be defined in <stddef.h> and few other C standard library headers that are inherited by c++. It may be defined by the implementation whether anything is included or not, since it is reserved, but relying on such non guaranteed definition would not be wise.

So I presume this technically means that the definition of size_t in the global namespace has been deprecated?

Indeed. More precisely, all standard headers that are guaranteed to define ::size_t are now deprecated in C++17. Same applies to all other non-macro C library names such as ::FILE.

Rohde answered 24/7, 2018 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.