初始化声明符是否为纯右值表达式

Is an init-declarator a prvalue expression

int c = 0;

考虑上面的代码,其中,c = 0是一个初始化声明符,也是一个表达式,因为这些规则:

init-declarator:
declarator initializer(opt)

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

只要是一个表达式,都会有一个值类别。

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.

a = 0的求值会初始化对象a。那么,全表达式c=0是纯右值表达式吗?如果理解有误,请指正。

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

这不是 C++ 解析的工作方式。 c = 0 本身可能是一个表达式(如果它在允许表达式的上下文中),但这不是 int c = 0; 的解析方式。您必须遵循实际的 C++ 语法规则。

int c = 0;是一个simple-declaration, containing a decl-specifier-seq and an optional init-declarator-list. The latter is a sequence of one or more init-declarator条款。这个语法有两个组成部分:一个声明符和一个可选的初始化器。从语法上讲,decl-specifier-seqint 所在的位置,declaratorc 部分,并且初始化器= 0位。

init-declarator 的文本在某些情况下可能被解析为表达式。但是被解析的内容是由 语法规则 决定的。并且 simple-declaration 的语法规则不允许 decl-specifier-seq 后跟 expression。因此,它后面的内容不会被解析为 表达式,即使它可能是。

因此 init-declarator 不是 表达式,即使文本看起来可能是。

现在,有一个"full-expression"的概念。被称为 "full-expressions" 的东西之一是 init-declarator 语法。

让您感到困惑的部分是 "full-expression" 和 表达式 之间的区别。 表达式specific piece of C++ grammar。完整表达式 不是 ;它是一个语言概念,包括许多不同的语法片段,但完整表达本身并不是语法。

因此,虽然语法结构 init-declarator 是 "full-expression" 而 不会 使它成为 表达式。语法结构 expression 定义明确,而 int c = 0; 不符合该语法。 init-declarator 可能包含一个表达式(或多个表达式,取决于initializer), 但它本身并不是一个 expression.

并且只有 表达式 具有值类别。因此,询问不是表达式的事物的值类别不是有效问题。