gpt4 book ai didi

c++ - 关于 C++ 中 sizeof 运算符的强大功能

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:33 24 4
gpt4 key购买 nike

我正在阅读现代 C++ 设计。下面的描述中提到了 sizeof operator。以下段落从泛型编程的角度进行解释。

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.

我的问题是作者的意思是什么 sizeof 返回它的大小而不实际评估运行时的表达式。在最后一行中还提到 sizeof 会丢弃表达式。请求帮助理解这些语句,如果能用示例完成就更好了。

谢谢

最佳答案

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

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

关于c++ - 关于 C++ 中 sizeof 运算符的强大功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5126518/

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