我知道什么是“表达类别”,但我对“表达类型”感到困惑。
Expression evaluation may produce a result.
那么,结果的类型就是“表达式类型”吗?
但是,也根据 www.cppreference.com :,它说:
Each C++ expression is characterized by two independent properties: A type and a value category.
那么,“type”是指表达式的类型,还是表达式结果的类型?
我想这可能需要一些编译原理的知识(我对此一无所知),到底是先判断表达式的类型,然后根据它来判断表达式结果的类型?或者表达式结果的类型就是表达式的类型?
请从编译原理的角度给出一个通俗易懂的解释。
最佳答案
Each C++ expression is characterized by two independent properties: A type and a value category.
那么,“type”是否指的是表达式的类型
block 引用>是的。 “type”确实指的是表达式的类型。
So, is the type of the result the "type of expression"?
不一定。表达式和表达式的结果是不同的东西。在某些情况下,表达式及其结果确实具有相同的类型,而在其他情况下,这些类型不同。此外,有些表达式没有任何结果。
表达式的结果在标准中定义如下:
[basic.lval]
The result of a glvalue is the entity denoted by the expression. The result of a prvalue is the value that the expression stores into its context; a prvalue that has type cv void has no result. A prvalue whose result is the value V is sometimes said to have or name the value V. The result object of a prvalue is the object initialized by the prvalue; a non-discarded prvalue that is used to compute the value of an operand of a built-in operator or a prvalue that has type cv void has no result object.
表达式的类型是为每种表达式定义的。例如:
[expr.unary.op]
The unary * operator performs indirection. Its operand shall be a prvalue of type “pointer to T”, where T is an object or function type. The operator yields an lvalue of type T denoting the object or function to which the operand points.
高亮部分表示该表达式的类别为左值,该表达式的类型为
T
.例如:
int i = 0; int* p = &i; int j = *p;
表达式的类型
*p
是int
根据引用的规则。它是一个左值,结果是对象 - 也由变量i
命名。 - 其类型也是int
.此外,
[expr.type]
部分指定可以调整任何类型表达式的类型的规则。
关于c++ - C++中的 "type of expression"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76872148/