The operator removes associativity

While searching for operator associativity on wikipedia, I noticed that delete has associativity from right to left. The source is quoted as msdn , I checked it, and it falls under the priority of group 3, from right to left associativity. So I checked the C ++ standard (n4296)

5.3 Unary expressions [expr.unary]

1) Expressions with unary groups of operators from right to left

 unary-expression: postfix-expression ++ cast-expression -- cast-expression unary-operator cast-expression sizeof unary-expression sizeof ( type-id ) sizeof ... ( identifier ) alignof ( type-id ) noexcept-expression new-expression delete-expression unary-operator: one of * & + - ! ~ 

What consequences does this have? What does delete have any associativity?

+5
source share
3 answers

As Barry said, priority is determined by grammar (and there are several operators that do not fit the basic idea of โ€‹โ€‹priority very well, so you can only really figure out what happens completely correctly from the grammar, not the priority table).

Even if we ignore it, however, the priority of deletion only (at least usually) determines whether the statement is legal / permitted, and not what it means. To give a counterexample, with + and * , priority determines that 2 * 3 + 4 gives 10, not 14 (i.e. Multiplication takes precedence).

In the case of delete , no value is created as a result of the delete operator; therefore, a statement of the type delete x + y; just not allowed. It will be parsed as (delete x) + y; , but since delete x does not create a value that can be added to anything else, the result is always inhibited (and if you change the statement, this will remain true).

Associativity doesn't really make sense for delete . In particular, associativity deals with what will look like: a @ b @ c like (a @ b) @ c or a @ (b @ c) (where @ is some operator). This is really real for operators that accept two operands. It is simply impossible to combine delete in such a way that you can ask a question (questions) to which associativity answers.

+6
source

The C ++ standard usually does not define operators in terms of priority or associativity. He defines them in terms of grammar. From [expr.delete], delete used in the delete expression, which is defined as:

delete-expression:
:: opt delete cast-expression
:: opt delete [] cast-expression

If expression-expression is defined in [expr.cast]:

monolithic expression:
Unary expression
( type-id ) cast-expression

And a unary expression is a whole bunch of things defined by [expr.unary], which are all unary expressions (increments, decrements, delete )

That is, delete *x is an association from right to left because (delete (*x)) is the only way to parse this expression according to grammar.

This is also the reason why cppreference quotes delete priority, where this happens is a direct consequence of this. For example, delete higher than + because in an expression like this:

 delete x+y 

x+y not a unitary expression, so the only legal grammar analysis is (delete x) + y .

+2
source

Associativity is related to whether a op b op c analyzed as (a op b) op c or a op (b op c) .

delete is a unary operator, so it cannot be associated. He has no associativity.

And delete delete x never acts.

+1
source

All Articles