size_t和sizeof不一致

时间:2022-04-25 17:06:53

Isn't it bothersome that the result of a native operator cannot be defined without including a header file?

不包含头文件就不能定义本机操作符的结果,这不是很麻烦吗?

According to this page, size_t is defined in headers cstddef, cstdio, cstring, ctime, and cstdlib. Thus, if neither of those headers is included then size_t should be undefined. However, the following program compiles without any warning (using MSVC 2015RC).

根据这个页面,size_t定义在header cstddef、cstdio、cstring、ctime和cstdlib中。因此,如果这两个头都不包含,那么size_t应该是未定义的。但是,以下程序在没有任何警告的情况下编译(使用MSVC 2015RC)。

int main()
{
    auto d_Size = sizeof( int );
    return 0;
}

It seems that size_t is somewhat of a bastard between a native type and a typedef. What does the standard say?

看起来size_t有点像原生类型和typedef之间的私生子。标准是怎么说的?

2 个解决方案

#1


31  

5.3.3 Sizeof [expr.sizeof]

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 ]

1) sizeof运算符在其操作数的对象表示中产生字节数。操作数可以是一个表达式,它是一个未计算的操作数(第5条),也可以是一个带括号的类型id。对于具有函数或不完整类型的表达式、此类类型的圆括号名或指定位字段的glvalue,不应应用sizeof运算符。sizeof(char)、sizeof(signed char)和sizeof(unsigned char)是1。应用于任何其他基本类型(3.9.1)的sizeof的结果是由实现定义的。[注意:特别是,sizeof(bool)、sizeof(char16_t)、sizeof(char32_t)和sizeof(wchar_t)都是实现定义的。[注:字节定义见1.7,对象表示定义见3.9。——结束注意)

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 ]

6) sizeof和sizeof…是std: size_t类型的常数。[注:std::size_t定义在标准头 (18.2)中。——结束注意)

However, std::size_t is just a type alias. The sizeof operator can return its result without any need of "accessing" the type alias; the result of sizeof is some fundamental type (implementation defined), which is then aliased as std::size_t in <cstddef>.

但是,std::size_t只是一个类型别名。sizeof运算符可以返回其结果,而不需要“访问”类型别名;sizeof的结果是一些基本类型(定义的实现),然后被作为std::size_t在 中。

Note also that in C++ typedef or using do not define a new type (i.e. a strong type), but only an alias (i.e. their typeid are the same). Hence, in your case, auto just deduces the fundamental type returned by the sizeof operator, which is the same as the type alias std::size_t. No problem for the compiler.

还请注意,在c++ typedef或using中,不定义新类型(即强类型),而只定义一个别名(即它们的类型id相同)。因此,在您的例子中,auto只推导出sizeof运算符返回的基本类型,它与类型别名std::size_t相同。编译器没问题。

#2


4  

According to the C++ standard, std::size_t is defined in <cstddef>.

根据c++标准,std::size_t在 中定义。

5.3.3 Sizeof

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 ]

6 . sizeof和sizeof…是std: size_t类型的常数。[注:std::size_t定义在标准头 (18.2)中。——结束注意)

#1


31  

5.3.3 Sizeof [expr.sizeof]

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 ]

1) sizeof运算符在其操作数的对象表示中产生字节数。操作数可以是一个表达式,它是一个未计算的操作数(第5条),也可以是一个带括号的类型id。对于具有函数或不完整类型的表达式、此类类型的圆括号名或指定位字段的glvalue,不应应用sizeof运算符。sizeof(char)、sizeof(signed char)和sizeof(unsigned char)是1。应用于任何其他基本类型(3.9.1)的sizeof的结果是由实现定义的。[注意:特别是,sizeof(bool)、sizeof(char16_t)、sizeof(char32_t)和sizeof(wchar_t)都是实现定义的。[注:字节定义见1.7,对象表示定义见3.9。——结束注意)

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 ]

6) sizeof和sizeof…是std: size_t类型的常数。[注:std::size_t定义在标准头 (18.2)中。——结束注意)

However, std::size_t is just a type alias. The sizeof operator can return its result without any need of "accessing" the type alias; the result of sizeof is some fundamental type (implementation defined), which is then aliased as std::size_t in <cstddef>.

但是,std::size_t只是一个类型别名。sizeof运算符可以返回其结果,而不需要“访问”类型别名;sizeof的结果是一些基本类型(定义的实现),然后被作为std::size_t在 中。

Note also that in C++ typedef or using do not define a new type (i.e. a strong type), but only an alias (i.e. their typeid are the same). Hence, in your case, auto just deduces the fundamental type returned by the sizeof operator, which is the same as the type alias std::size_t. No problem for the compiler.

还请注意,在c++ typedef或using中,不定义新类型(即强类型),而只定义一个别名(即它们的类型id相同)。因此,在您的例子中,auto只推导出sizeof运算符返回的基本类型,它与类型别名std::size_t相同。编译器没问题。

#2


4  

According to the C++ standard, std::size_t is defined in <cstddef>.

根据c++标准,std::size_t在 中定义。

5.3.3 Sizeof

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 ]

6 . sizeof和sizeof…是std: size_t类型的常数。[注:std::size_t定义在标准头 (18.2)中。——结束注意)