关于 C++ 中 sizeof 运算符的幂
我正在阅读《现代 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这意味着
sizeof(1/0)
将产生sizeof(int)
,即使1/0
通常会中止程序,因为除法为零是一个运行时错误。此外,对于任何声明为T* p
的p
,sizeof(*p)
将产生sizeof(T)
无论p
中存储什么值,即使p
悬空或根本没有初始化。It means that
sizeof(1/0)
will yieldsizeof(int)
, even though1/0
would normally abort the program, because division by zero is a runtime error. Also, for anyp
declared asT* p
,sizeof(*p)
will yieldsizeof(T)
no matter what value is stored inp
, even ifp
is dangling or not initialized at all.sizeof
在编译时进行计算:编译器计算sizeof
运算符后面的表达式的类型。这是由编译器一劳永逸完成的,因此句子“无需在运行时实际评估该表达式”。编译器计算类型,然后能够从类型推导出表达式的大小,然后,仍然在编译时,整个
sizeof< /code> 表达式被计算的大小替换。因此表达式本身不会进入可执行代码。这就是句子“sizeof 丢弃表达式并仅返回其结果的大小” 的含义。
sizeof
is evaluated at compile time: the compiler computes the type of the expression that follows thesizeof
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.下面给出了
i++
类型的 sizeof,即int
(通常 int 有 4 或 8 个字节,因此它可能会给你值 4 或 8 )。但是,由于未对表达式求值,因此不会对该表达式执行任何运行时操作。计算表达式基本上意味着执行其副作用(例如递增变量)或在运行时从内存或寄存器读取值。因此,从某种意义上说,sizeof“扔掉”了它的操作数,因为它并没有真正执行它指定的运行时操作(i的值仍然为零)。
The following gives you the sizeof of the type that
i++
has, which isint
(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.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 ofi
will still be zero).编译器需要计算各种操作的类型/结构/类的大小。
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 dosizeof(int)
the compiler knows how big anint
is (in bytes) and will insert that value instead. The same applies for more complex things likesizeof(myVariable)
withmyVariable
being of typeMyClass
: the compiler does know how much spaceMyClass
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.
这意味着
int j=sizeof(int);
将被编译为int j=4;
我已经阅读了编译后的程序集,在执行过程中实际上没有计算!
It means
int j=sizeof(int);
would be compiled toint j=4;
I have read the compiled assembly, there is no actually calc during execution!