Are "Primary Expressions" and "Operands" the same thing?

128 views
Skip to first unread message

Yar Izdum

unread,
Sep 2, 2016, 10:22:51 PM9/2/16
to ISO C++ Standard - Discussion
This is a question from a newcomer still learning the language. A lot of you may find the question tiresome. I think over the years the terms "operand" and "expression" have become so common that an assumption has been made that everyone understands them perfectly. As I learn the language, I've come to the understanding that an operand is a value that operators work with in expressions. Sometimes the operator is absent (discard-value expression), but regardless the operand evaluates to a value. So then I begin to wonder if "operand" and "expression" are the exact same thing. Maybe this is true, but it is never made clear, no matter what source I'm reading and learning from. Now I'm seeing the term "Primary Expression," which seems to be a fancier word for "operand," and so my confusion begins. Primary Expressions list literals and variables among them, which I am familiar with, and expressions between parenthesis (which I assume means that the parenthesis forces the expression to evaluate fully so it can be treated as a single operand?), and several advanced entities like the keyword "this" which I am familiar with, but classes are something I've dabbled in, but don't fully understand yet. The term "Primary Expression" isn't clear to me, but I have an idea (which could be wrong). I'm hoping someone here can make it clear for me.

There's also one other term, "subexpression." I've written out something, like an example, and I'd like someone here to tell me if I'm correct.

A subexpression is each part of a full-expression that must be individually evaluated before a final result can be determined.

For instance, 5 * 8 / 2 + 5 is a full-expression. 5 * 8 is a subexpression that evaluates to 40, 40 / 2 is a subexpression that evaluates to 20, and 20 + 5 is a subexpression that evaluates to 25, the final result.


Also, looking at that, I had pondered if even the literals 5, 8, 2, and 5 are also subexpressions. But I really don't know for certain.




Belloc

unread,
Sep 3, 2016, 11:23:39 AM9/3/16
to ISO C++ Standard - Discussion
Operands are always sub-expressions of other expressions containing them. Discarded-value expressions are not necessarily without operators. For instance, the expression a + b; is a discarded-value expression. As the name implies, its value is discarded at the end of its calculation. See [expr]/12.

Belloc

unread,
Sep 3, 2016, 11:30:30 AM9/3/16
to ISO C++ Standard - Discussion


On Friday, September 2, 2016 at 11:22:51 PM UTC-3, Yar Izdum wrote:
I forgot to answer your question in the title: No, a primary expression is not the same as an operand. For example, a; is a primary expression, but its not an operand. See also [expr.prim].

Yar Izdum

unread,
Sep 3, 2016, 10:30:08 PM9/3/16
to ISO C++ Standard - Discussion
Thank you, Belloc!
The reason that the term "subexpression" is confusing to newcomers: The term "full expression" is defined in several books I've been reading (and the standard draft also defines the word), but "subexpression," for example in the book "C++ Primer" is only defined in the section about Regular Expressions (something far out of my depth, for now). I think the terms "expression," "full expression," "subexpression," and "Primary Expression," well, it would be nice if they were more fleshed out so newcomers don't get confused. Someone who's been into C++ and programming for 20 years would shrug at this, but someone who's just getting started is probably going, "Huh, what?"

FrankHB1989

unread,
Sep 4, 2016, 11:05:03 PM9/4/16
to ISO C++ Standard - Discussion


在 2016年9月3日星期六 UTC+8下午11:30:30,Belloc写道:


I forgot to answer your question in the title: No, a primary expression is not the same as an operand. For example, a; is a primary expression, but its not an operand. See also [expr.prim].
`a` in `a;` can be an expression, but `a;` itself is not. It is probable a statement.

Yar Izdum

unread,
Sep 5, 2016, 2:57:49 AM9/5/16
to ISO C++ Standard - Discussion
Where did the term "Primary Expression" come from? It's not really covered in books that teach the language. Microsoft's documentation on C++ covers the topic only slightly better than the standard draft: https://msdn.microsoft.com/en-us/library/z94za21a.aspx

I understand the standard is not meant for programmers, exactly, it is meant for compiler developers. But while I can read the standard and understand almost all of it, the sections on "expressions" confuse the heck out of me because of this unfamiliar territory As I mentioned, Primary Expressions are not covered in C++ programmer books, and the word "subexpression" gets thrown around with two different meanings, one being "each part of a full expression" and the other having to do with RegEx (regular expression subexpressions are between parentheses). I've been looking into this subject for weeks, now, with little success. I can't figure how I can understand everything else BUT this. I can even grasp the new "value categories," but not this. Something must be wrong with me lately. Hopefully it will come to me in time.

Johannes Sixt

unread,
Sep 5, 2016, 3:24:53 PM9/5/16
to std-dis...@isocpp.org
Am 05.09.2016 um 08:57 schrieb Yar Izdum:
> Where did the term "Primary Expression" come from? It's not really
> covered in books that teach the language.

"Primary expression" is a term that is only relevant in the context of
language definition and compiler construction. You should not come
across the term during day-to-day programming.

In its context, "primary expressions" usually denote the parts of an
expression that are at the top of the evaluation priority. These are
variables, literals, and expressions nested in parentheses.

> Primary Expressions are not covered in C++ programmer books, and the
> word "subexpression" gets thrown around with two different meanings, one
> being "each part of a full expression" and the other having to do with
> RegEx (regular expression subexpressions are between parentheses).

"Sub-expression" is an informal term. It means whatever common language
suggests in the context that it should mean. When you talk about
expressions in C++ language, a sub-expression is a part of another
expression (it usually implies that the sub-expression is syntactically
complete). When you talk about regular expressions, then a
sub-expression is a part of a regular expression.

Yar Izdum

unread,
Sep 6, 2016, 2:03:54 AM9/6/16
to ISO C++ Standard - Discussion, j...@kdbg.org
I know that C and C++ are not really the same language, but C++ has its roots in C, so I'm curious why Microsoft defines the term "operands" as "Primary Expressions": https://msdn.microsoft.com/en-us/library/40b07dd5.aspx

I would not know about Primary Expressions at all, if I had never looked at the standard draft. Now I'm trying to remember why I did...
It was probably curiosity, but it feels like I looked in a place where I should not have. At least not until I comprehend the majority of the language, and the standard is intimidating to someone like me.
Oh, I do remember now. I had been under the assumption that an "expression" always resulted in a single value. Turns out I was wrong, and the standard defines an expression as "a sequence of operands and operators that MAY compute a value." I had wondered why a function with a return type of void could be considered an expression since it can't even be used in an arithmetic expression, like:

#include <iostream>

void doNothing()
{
}

int main()
{
    std::cout << 5 + doNothing(); // won't compile
    return 0;
}

This is what led me down the rabbit hole. And it's weird, but since the statement: 5;    is an expression (a useless one, but still valid), the definition "sequence of operators and operands" isn't quite right, either. Or is it? The more you know, the more questions you have, right? That's me.

FrankHB1989

unread,
Sep 6, 2016, 8:41:39 AM9/6/16
to ISO C++ Standard - Discussion, j...@kdbg.org
It seems that Microsoft did not review their documentation very carefully to keep it conforming to standards' speech. Searching for older documentation (like for VC++ 6), you will find more glitches.

Most materials on MSDN are even not meet to general expectation of technical specifications. Even in their newest versions of C# specification, they go against to ECMA-334, insist on (obviously problematic) "destructors" rather than "finalizers" terminology.

So basically, don't feel strange. You don't have to take it too serious (besides you can't do much otherwise).

在 2016年9月6日星期二 UTC+8下午2:03:54,Yar Izdum写道:

Yar Izdum

unread,
Sep 7, 2016, 8:01:15 PM9/7/16
to ISO C++ Standard - Discussion
Hm. Would you say that every operand in C++ is also an expression (although not every expression can be an operand)?

Belloc

unread,
Sep 8, 2016, 7:42:47 AM9/8/16
to ISO C++ Standard - Discussion
Yes. Although, to be more precise, I would say every operand is a sub-expression of the expression containing it.

Richard Smith

unread,
Sep 8, 2016, 1:45:14 PM9/8/16
to std-dis...@isocpp.org
On Wed, Sep 7, 2016 at 5:01 PM, Yar Izdum <jeremywa...@gmail.com> wrote:
Hm. Would you say that every operand in C++ is also an expression (although not every expression can be an operand)?

The term "operand" is defined by ISO/IEC 2382, to which ISO C++ has a normative reference. The definition is:

  operand
  entity on which an operation is performed

Informally, it seems accurate to say that every operand in a C++ expression is either an expression or a braced-init-list (the right-hand side of an assignment, for instance, is an operand of that operator, but is not necessarily an expression).

On Saturday, September 3, 2016 at 11:23:39 AM UTC-4, Belloc wrote:

Operands are always sub-expressions of other expressions containing them. Discarded-value expressions are not necessarily without operators. For instance, the expression a + b; is a discarded-value expression. As the name implies, its value is discarded at the end of its calculation. See [expr]/12.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

Message has been deleted

Belloc

unread,
Sep 8, 2016, 7:02:34 PM9/8/16
to ISO C++ Standard - Discussion


On Thursday, September 8, 2016 at 2:45:14 PM UTC-3, Richard Smith wrote:

The term "operand" is defined by ISO/IEC 2382, to which ISO C++ has a normative reference. The definition is:

  operand
  entity on which an operation is performed

Informally, it seems accurate to say that every operand in a C++ expression is either an expression or a braced-init-list (the right-hand side of an assignment, for instance, is an operand of that operator, but is not necessarily an expression).

I think we are splitting hairs a bit here, as the term operand is not formally defined in the C++ Standard, but I have to agree with you that a braced-init-list is not an expression, when it's used as a initializer-clause of an assignment-expression. 

Richard Smith

unread,
Sep 8, 2016, 7:30:39 PM9/8/16
to std-dis...@isocpp.org
On Thu, Sep 8, 2016 at 4:02 PM, Belloc <jabe...@gmail.com> wrote:


On Thursday, September 8, 2016 at 2:45:14 PM UTC-3, Richard Smith wrote:

The term "operand" is defined by ISO/IEC 2382, to which ISO C++ has a normative reference. The definition is:

  operand
  entity on which an operation is performed

Informally, it seems accurate to say that every operand in a C++ expression is either an expression or a braced-init-list (the right-hand side of an assignment, for instance, is an operand of that operator, but is not necessarily an expression).

I think we are splitting hairs a bit here, as the term operand is not formally defined in the C++ Standard,

As noted, the C++ standard has a normative reference to ISO/IEC 2382, which defines the term, so it is formally defined for use in the C++ standard.
 
but I have to agree with you that a braced-init-list is not an expression, when it's used as a initializer-clause of an assignment-expression. 

--

Yar Izdum

unread,
Sep 9, 2016, 5:21:30 AM9/9/16
to ISO C++ Standard - Discussion

The term "operand" is defined by ISO/IEC 2382, to which ISO C++ has a normative reference. The definition is:

  operand
  entity on which an operation is performed

Informally, it seems accurate to say that every operand in a C++ expression is either an expression or a braced-init-list (the right-hand side of an assignment, for instance, is an operand of that operator, but is not necessarily an expression).


Hm. So would this underlined bit be true as well, then? Sorry to keep dragging this topic through the mud. The specifics are causing my brain to cave in a little...

    An expression can be as simple as a single literal or variable. The result of such an expression is the value of the literal or variable. When there are no operators present to act on them, literals and variables are not operands; they are expressions.


  1. // The simplest form of an expression is a single literal or variable:
  2. #include <iostream>
  3.     
  4. int main()
  5. {
  6.     // Declares a variable of type int named "whole_number":
  7.     int whole_number;
  8.     
  9.     // The variable "whole_number" and integer literal 7 are operands of the assignment operator (=):
  10.     whole_number = 7;
  11.     
  12.     // "whole_number" is an expression that evaluates to the value stored (7):
  13.     whole_number;
  14.     
  15.     // The integer literal 7 is an expression that evaluates to its value (7):
  16.     7;
  17.     
  18.     return 0;
  19. }
 

Richard Smith

unread,
Sep 9, 2016, 1:45:05 PM9/9/16
to std-dis...@isocpp.org
On Fri, Sep 9, 2016 at 2:21 AM, Yar Izdum <jeremywa...@gmail.com> wrote:

The term "operand" is defined by ISO/IEC 2382, to which ISO C++ has a normative reference. The definition is:

  operand
  entity on which an operation is performed

Informally, it seems accurate to say that every operand in a C++ expression is either an expression or a braced-init-list (the right-hand side of an assignment, for instance, is an operand of that operator, but is not necessarily an expression).


Hm. So would this underlined bit be true as well, then? Sorry to keep dragging this topic through the mud. The specifics are causing my brain to cave in a little...

    An expression can be as simple as a single literal or variable. The result of such an expression is the value of the literal or variable. When there are no operators present to act on them, literals and variables are not operands; they are expressions.


Let's take a step back here: why do you care? What rule of the language involving "operands" are you trying to understand? (I think you were referring to the note in [expr] paragraph 1 in an earlier message; is that the sentence you're trying to understand?)
 
  1. // The simplest form of an expression is a single literal or variable:
  2. #include <iostream>
  3.     
  4. int main()
  5. {
  6.     // Declares a variable of type int named "whole_number":
  7.     int whole_number;
  8.     
  9.     // The variable "whole_number" and integer literal 7 are operands of the assignment operator (=):
  10.     whole_number = 7;
  11.     
  12.     // "whole_number" is an expression that evaluates to the value stored (7):
  13.     whole_number;
  14.     
  15.     // The integer literal 7 is an expression that evaluates to its value (7):
  16.     7;
  17.     
  18.     return 0;
  19. }
 

--

Yar Izdum

unread,
Sep 9, 2016, 8:09:45 PM9/9/16
to ISO C++ Standard - Discussion

Let's take a step back here: why do you care? What rule of the language involving "operands" are you trying to understand? (I think you were referring to the note in [expr] paragraph 1 in an earlier message; is that the sentence you're trying to understand?)



Fair enough. Initially I was trying to understand "Primary Exressions." As others have said, it's not a word you hear outside the standard.
As I understand it now, primary expressions are the first values/objects to evaluate in any expression. This led me to think they were operands, but this is not so... trying to think of a reason why not, I got this answer:

variable; // <-- expression, not operand

So I came to the conclusion that:
variable + variable; // <-- variable is an operand, AND an expression
variable; // <-- variable is an expression, but not an operand since no operators are present

As for why I care, I'm going to have to assume I have an abnormal way of thinking, but I am also on half a dozen psych meds too. It could be OCD and perfectionism. If it's not important to know this, that's fine also.
Reply all
Reply to author
Forward
0 new messages