关于 C++ 中 sizeof 运算符的幂

发布于 2024-10-19 14:48:40 字数 382 浏览 5 评论 0原文

我正在阅读《现代 C++ 设计》。在下面的描述中提到了关于操作符的大小。以下段落从通用编程的角度进行解释。

sizeof 具有令人惊讶的强大功能:您可以将 sizeof 应用于任何表达式,无论多么复杂,并且 sizeof 返回其大小,而无需在运行时实际评估该表达式。这意味着 sizeof 知道重载、模板实例化、转换规则——所有可以参与 C++ 表达式的内容。事实上,sizeof 隐藏了推导表达式类型的完整功能;最终,sizeof 丢弃表达式并仅返回其结果的大小。

我的问题是作者的意思是 sizeof 返回其大小而不在运行时实际评估表达式。在最后一行中还提到 sizeof 丢弃了表达式。请求帮助理解这些陈述,如果能举例说明就更好了。

谢谢

I am reading Modern C++ design. It was mentioned about sizeof opeator as following description. Following paragraph is explained from generic programming point of view.

There is a surprising amount of power in sizeof: You can apply sizeof to any expression, no matter how complex, and sizeof returns its size without actually evaluating that expression at runtime. This means that sizeof is aware of overloading, template instantiation, conversion rules—everything that can take part in a C++ expression. In fact, sizeof conceals a complete facility for deducing the type of an expression; eventually, sizeof throws away the expression and returns only the size of its result.

My question is what does author mean sizeof returns its size with out actually evalutating the exression at runtime. And also in last line it was mentioned that sizeof throws away the expression. Request help in understanding these statements, it would be good if it is done with example.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苦笑流年记忆 2024-10-26 14:48:40

作者的意思是 sizeof 返回其大小而不在运行时实际评估表达式。

这意味着 sizeof(1/0) 将产生 sizeof(int),即使 1/0 通常会中止程序,因为除法为零是一个运行时错误。此外,对于任何声明为 T* ppsizeof(*p) 将产生 sizeof(T)无论 p 中存储什么值,即使 p 悬空或根本没有初始化。

what does author mean sizeof returns its size with out actually evalutating the exression at runtime.

It means that sizeof(1/0) will yield sizeof(int), even though 1/0 would normally abort the program, because division by zero is a runtime error. Also, for any p declared as T* p, sizeof(*p) will yield sizeof(T) no matter what value is stored in p, even if p is dangling or not initialized at all.

人生百味 2024-10-26 14:48:40

sizeof 在编译时进行计算:编译器计算 sizeof 运算符后面的表达式的类型。这是由编译器一劳永逸完成的,因此句子“无需在运行时实际评估该表达式”

编译器计算类型,然后能够从类型推导出表达式的大小,然后,仍然在编译时,整个 sizeof< /code> 表达式被计算的大小替换。因此表达式本身不会进入可执行代码。这就是句子“sizeof 丢弃表达式并仅返回其结果的大小” 的含义。

sizeof is evaluated at compile time: the compiler computes the type of the expression that follows the sizeof operator. This is done once and for all by the compiler, hence the sentence “without actually evaluating that expression at runtime”.

The compiler computes the type, then it is able to deduce the size of the expression from the type, and then, still at compile time, the whole sizeof expression is replaced by the calculated size. So the expression itself does not make it into the executable code. That's what the sentence “sizeof throws away the expression and returns only the size of its result” means.

幸福丶如此 2024-10-26 14:48:40

下面给出了 i++ 类型的 sizeof,即 int (通常 int 有 4 或 8 个字节,因此它可能会给你值 4 或 8 )。但是,由于未对表达式求值,因此不会对该表达式执行任何运行时操作。

int i = 0;
sizeof(i++);

计算表达式基本上意味着执行其副作用(例如递增变量)或在运行时从内存或寄存器读取值。因此,从某种意义上说,sizeof“扔掉”了它的操作数,因为它并没有真正执行它指定的运行时操作(i的值仍然为零)。

The following gives you the sizeof of the type that i++ has, which is int (usually an int has 4 or 8 bytes, so it will likely give you value 4 or 8). However, since the expression is not evaluated, no runtime action is done for the expression.

int i = 0;
sizeof(i++);

Evaluating an expression basically means to execute its side effects (e.g incrementing a variable) or reading values from memory or registers at runtime. So in some sense sizeof "throws away" its operand, since it does not really perform the runtime operation it specifies (the value of i will still be zero).

大姐,你呐 2024-10-26 14:48:40

编译器需要计算各种操作的类型/结构/类的大小。 sizeof 运算符使这些大小可作为常量供您的程序使用。例如,如果您执行 sizeof(int) ,编译器就会知道 int 有多大(以字节为单位),并将插入该值。这同样适用于更复杂的事情,例如 sizeof(myVariable) ,其中 myVariable 的类型为 MyClass:编译器确实知道有多少空间 MyClass 占用了空间,因此可以插入该值。

关键是这个评估发生在编译时:结果是一个数字。在运行时,不需要再次进行评估。

The compiler needs to calculate the sizes of types/structs/classes for various operations. The sizeof operator makes these sizes available to your program as a constant. So for example, if you do sizeof(int) the compiler knows how big an int is (in bytes) and will insert that value instead. The same applies for more complex things like sizeof(myVariable) with myVariable being of type MyClass: the compiler does know how much space MyClass takes up and thus can insert that value.

The point is that this evaluation takes places at compile-time: the result is a number. During runtime, the evaluation does not need to be done again.

嘿咻 2024-10-26 14:48:40

这意味着 int j=sizeof(int); 将被编译为 int j=4;

我已经阅读了编译后的程序集,在执行过程中实际上没有计算!

It means int j=sizeof(int); would be compiled to int j=4;

I have read the compiled assembly, there is no actually calc during execution!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文