Is an init-declarator a prvalue expression

一曲冷凌霜 提交于 2020-06-16 23:48:20

问题


int c = 0;

Consider the above code,thereof,c = 0 is an init-declarator and it's also an expression,Becuase of these rules:

init-declarator:
declarator initializer(opt)

A full-expression is:
[...]
an init-declarator or a mem-initializer, including the constituent expressions of the initializer,

As long as an expression,it will have a value category.

A prvalue is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.

The evaluation of a = 0 will initialize object a.So, Is the full-expression c=0 a prvalue expression?If I misunderstand it,please correct me.


回答1:


Consider the above code,thereof,c = 0 is an init-declarator and it's also an expression

That's not how C++ parsing works. c = 0 by itself may be an expression (if it is within a context where expressions are allowed), but that's not how int c = 0; gets parsed. You have to follow the actual C++ grammar rules.

int c = 0; is a simple-declaration, containing a decl-specifier-seq and an optional init-declarator-list. The latter is a sequence of one or more init-declarator terms. And this grammar has two components: a declarator and an optional initializer. Grammatically speaking, the decl-specifier-seq is where int goes, the declarator is the c part, and the initializer is the = 0 bit.

The text of an init-declarator is something that may in some cases be parsed as an expression. But what something is parsed as is determined by the grammar rules. And the grammar rules of simple-declaration does not allow a decl-specifier-seq followed by expression. Therefore, what follows it is not parsed as an expression even if it could be.

So init-declarator is not an expression, even if the text looks like it could be.

Now, there is the concept of a "full-expression". One of the things that get to be called a "full-expressions" are init-declarator grammar.

The part that's confusing you is the difference between a "full-expression" and an expression. An expression is a specific piece of C++ grammar. A full-expression is not; it's a language concept which includes a number of different pieces of grammar, but full-expression is not itself grammar.

Therefore, while the grammatical construct init-declarator is a "full-expression" that does not make it an expression. The grammar construct expression is well defined, and int c = 0; doesn't fit that grammar. The init-declarator may contain an expression (or multiple expressions, depending on the initializer), but it is not itself an expression.

And only expressions have value categories. Therefore, asking about the value category of a thing which is not an expression is not a valid question.



来源:https://stackoverflow.com/questions/62077796/is-an-init-declarator-a-prvalue-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!