gpt4 book ai didi

c++ - size_t 和 sizeof 不一致

转载 作者:IT老高 更新时间:2023-10-28 12:45:47 28 4
gpt4 key购买 nike

不包含头文件就无法定义native算子的结果不是很麻烦吗?

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

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

似乎 size_t 有点像是原生类型和 typedef 之间的 SCSS 。标准是怎么说的?

最佳答案

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运算符无需“访问”类型别名即可返回其结果; sizeof 的结果是一些基本类型(实现定义),然后别名为 std::size_t<cstddef> .

另请注意,在 C++ 中 typedefusing 不定义新类型(即强类型),而只定义别名(即它们的typeid 相同)。因此,在您的情况下,auto只是推断 sizeof 返回的基本类型运算符,与类型别名 std::size_t 相同.编译器没问题。

关于c++ - size_t 和 sizeof 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30206065/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com