size_t 和 sizeof 不一致
Inconsistency for size_t and sizeof
如果不包含 header 文件就无法定义本机运算符的结果,这不是很麻烦吗?
根据此 page,size_t
定义在 headers cstddef、cstdio、cstring、ctime 和 cstdlib 中。因此,如果那些 header 都不包含,那么 size_t
应该是未定义的。但是,以下程序编译时没有任何警告(使用 MSVC 2015RC)。
int main()
{
auto d_Size = sizeof( int );
return 0;
}
size_t
似乎有点介于原生类型和 typedef 之间。标准怎么说?
5.3.3 Sizeof [expr.sizeof]
1) The sizeof operator yields the number of bytes in the object
representation of its operand. The operand is either an expression,
which is an unevaluated operand (Clause 5), or a parenthesized
type-id. The sizeof operator shall not be applied to an expression
that has function or incomplete type, to the parenthesized name of
such types, or to a glvalue that designates a bit-field. sizeof(char),
sizeof(signed char) and sizeof(unsigned char) are 1. The result of
sizeof applied to any other fundamental type (3.9.1) is
implementation-defined. [ Note: in particular, sizeof(bool),
sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are
implementation-defined.75 — end note ] [ Note: See 1.7 for the
definition of byte and 3.9 for the definition of object
representation. — end note ]
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 ]
然而,std::size_t
只是一个类型别名。 sizeof
运算符可以 return 其结果而不需要 "accessing" 类型别名; sizeof
的结果是一些基本类型(实现定义),然后在 <cstddef>
中别名为 std::size_t
。
另请注意,在 C++ 中 typedef
或 using
不要定义新类型(即 strong 类型),但只有一个别名(即它们的 typeid
是相同的)。因此,在您的情况下,auto
仅推导出由 sizeof
运算符编辑的基本类型 return,这与类型别名 std::size_t
相同。编译器没问题。
根据C++标准,std::size_t
定义在<cstddef>
中。
5.3.3 Sizeof
...
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 ]
如果不包含 header 文件就无法定义本机运算符的结果,这不是很麻烦吗?
根据此 page,size_t
定义在 headers cstddef、cstdio、cstring、ctime 和 cstdlib 中。因此,如果那些 header 都不包含,那么 size_t
应该是未定义的。但是,以下程序编译时没有任何警告(使用 MSVC 2015RC)。
int main()
{
auto d_Size = sizeof( int );
return 0;
}
size_t
似乎有点介于原生类型和 typedef 之间。标准怎么说?
5.3.3 Sizeof [expr.sizeof]
1) The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, to the parenthesized name of such types, or to a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75 — end note ] [ Note: See 1.7 for the definition of byte and 3.9 for the definition of object representation. — end note ]
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 ]
然而,std::size_t
只是一个类型别名。 sizeof
运算符可以 return 其结果而不需要 "accessing" 类型别名; sizeof
的结果是一些基本类型(实现定义),然后在 <cstddef>
中别名为 std::size_t
。
另请注意,在 C++ 中 typedef
或 using
不要定义新类型(即 strong 类型),但只有一个别名(即它们的 typeid
是相同的)。因此,在您的情况下,auto
仅推导出由 sizeof
运算符编辑的基本类型 return,这与类型别名 std::size_t
相同。编译器没问题。
根据C++标准,std::size_t
定义在<cstddef>
中。
5.3.3 Sizeof
...
6 The result of
sizeof
andsizeof...
is a constant of typestd::size_t.
[ Note:std::size_t
is defined in the standard header<cstddef>
(18.2). — end note ]