Take the following standard passage:
[C++11: 5.3.3/6]:
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>
(18.2). —end note ]
Now:
[C++11: 18.2/6]:
The type size_t
is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
Granted, the passage doesn't require that size_t
is a type alias defined with typedef
, but since it's explicitly stated to be made available by the standard header <cstddef>
, I think we can take as read that failing to include <cstddef>
should remove any guarantee that size_t
shall be available to a program.
However, according to that first quote, we can regardless obtain an expression of type std::size_t
.
We can actually demonstrate both of these facts:
int main()
{
typedef decltype(sizeof(0)) my_size_t;
my_size_t x = 0; // OK
std::size_t y = 1; // error: 'size_t' is not a member of 'std'
}
std::size_t
is not visible to the program, but sizeof(0)
still gives us one? Really?
Is it therefore not correct to say that 5.3.3/6
is flawed, and that it actually has "the same type as whatever std::size_t
resolves to", but not std::size_t
itself?
Sure, the two are one and the same if std::size_t
is a type alias but, again, nowhere is this actually required.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…