3
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.

xmh0511
  • 7,010
  • 1
  • 9
  • 36
  • 4
    Being a full-expression doesn't mean that it is an expression, oddly enough. – eerorika May 29 '20 at 02:44
  • @eerorika if a full-expression is not considered ,why there is something like `whose full-expression is a constant expression` in the standard?we know the prior condition for **constant expression** is it must be a **core constant expresion** – xmh0511 May 29 '20 at 02:50
  • 1
    Some full-expressions are expressions. Not all. – eerorika May 29 '20 at 02:51
  • @eerorika Could you please list some situation that full-expressions are expressions?or can be found in the standard? – xmh0511 May 29 '20 at 02:53
  • @jackX Are there any situations where you are unclear what rules that applies? Those would be interesting to blend into the mix. – Ted Lyngmo May 29 '20 at 02:54
  • @jackX All expressions that are not sub expressions are full-expressions for example. For example in the expression statement `i++;`, the expression `i++` is a full-expression. – eerorika May 29 '20 at 02:56
  • @TedLyngmo I just can't understand the distinction between `expression` and `full-expression`,in other words.The rules apply to **expression** can aslo apply to **full-expression**? – xmh0511 May 29 '20 at 02:56
  • @jackX Only if the full-expression is an expression. And not all full-expressions are expressions. – eerorika May 29 '20 at 02:57
  • @eerorika excatly.The full-expression `i++;` ,it's a prvalue expression.So why a arbitrary full-expression can't have a value category?Or,how to distinct which full-expression can't have a value category? – xmh0511 May 29 '20 at 02:59
  • @eerorika how to determine whether a `full-expression` is also a **expression** or it just a **full-expression**? – xmh0511 May 29 '20 at 03:00
  • @jackX It's not arbitrary. Expressions have a value category. `i++` is an expression, therefore it has a value category. `int i = 0;` is not an expression. Therefore it does not have a value category. Whether it is a full-expression is not is irrelevant. – eerorika May 29 '20 at 03:00
  • @jackX See the standard section [expr] which defines all expressions. if it is not defined in the section, then it is not an expression. Or check [gram.expr] section for a list, although the grammar can be confusing at times. – eerorika May 29 '20 at 03:01
  • @eerorika thanks.you mean that some rules in the standard that apply to **expression**,we can't apply the rule to **full-expression** arbitrarily,except that the **full-expression** is also a **expression**,Right? – xmh0511 May 29 '20 at 03:03
  • 2
    If this is going to be worth anything (in the long run) - please put up an answer. For a non-theorist like me, following your discussion gets me nowhere. :-) – Ted Lyngmo May 29 '20 at 03:05
  • 4
    @jackX Yes. Rules that apply to expressions only apply to expressions. If something is not an expression, then rules applying to expressions do not apply to it. There are full-expressions that are not expressions. Therefore there are rules that apply to expressions which do not apply to all full-expressions. – eerorika May 29 '20 at 03:05
  • @eerorika I see it,but how to interpret "the full-expression of the initialization shall be a constant expression",as I said,the **init-declarator** is only a full-expression,however,these rules in [expr.const] explicitly apply to **expression**,not to **full-expression**. – xmh0511 May 29 '20 at 03:09
  • @jackX That, I don't know ¯\\_(ツ)_/¯ – eerorika May 29 '20 at 03:14
  • @eerorika Does it mean that all expressions within the full-expression must be constant expression?otherwise,there's no way to interpret this sentence,because as you said ,the rules in [expr.const] only apply to expression... – xmh0511 May 29 '20 at 03:17
  • @jackX Perhaps. That would make sense. – eerorika May 29 '20 at 03:19

1 Answers1

3

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I have to say that `int` within `int c = 0;` is a **decl-specifier-seq**,the part `c =0` within `int c = 0;` is a **init-declarator**,because [dcl.decl#nt:init-declarator](https://timsong-cpp.github.io/cppwp/n4659/dcl.decl#nt:init-declarator) – xmh0511 May 29 '20 at 03:22
  • For `int c = 0;`,it has three components,[dcl.decl#2](https://timsong-cpp.github.io/cppwp/n4659/dcl.decl#2) – xmh0511 May 29 '20 at 03:23
  • @jackX: I've fixed the references and the text, but the point remains; you don't get to say "hey, this grammar looks like an expression, so it is one". You have to follow C++'s grammar rules. "*it has three components*" Yes, and not one of those three components is, by itself, an expression. Some of them may contain expressions, but they're not "expression" as far as the C++ grammar is concerned. – Nicol Bolas May 29 '20 at 03:31
  • Thanks for your answers,In short,you mean that these rules in the standart that apply to **expression**,we can't apply these rules to **full-expression** arbitrarily.Howerver,I have a confusion about the sentence "the full-expression of the initialization shall be a constant expression",we know these rules in [expr.const] apply to **expression**,so how to interpret this . A init-declarator is not an expression,how to determine whether it's a constant expression use these rules in [expr.const]? – xmh0511 May 29 '20 at 03:35
  • 1
    (jack, put a space after a punctuation ... it's hard to read now) – Ted Lyngmo May 29 '20 at 03:35
  • 1
    @jackX: Your question here is about the value category of a "full-expression". Now you're asking a different question about a specific use of the "full-expression" concept in some case. – Nicol Bolas May 29 '20 at 03:36
  • @NicolBolas All right,I have to ask another question.Appreciate you to give an answers,I will give a link later -:) – xmh0511 May 29 '20 at 03:47
  • @jackX: If you ask another question, please use proper spacing around punctuation. IE: not *no spacing*. – Nicol Bolas May 29 '20 at 03:48
  • @NicolBolas Hi, Nicol. Please give an answers for this [question](https://stackoverflow.com/questions/62078530/how-to-understand-the-sentence-full-expression-must-be-a-constant-expression-w) – xmh0511 May 29 '20 at 04:07
  • @jackX Your style is perfectly fine except for in that one comment. – Ted Lyngmo May 29 '20 at 08:42