索引表达式和指针解引用表达式的优先级

huangapple go评论99阅读模式
英文:

Precedence of index expression and pointer dereference expression

问题

给定一个简单的表达式,如

*a[0]

其中 a 声明为 *[]string(指向字符串切片的指针)。

标准文档中确切地解释了语言构造的评估顺序吗?

我发现它们中没有一个实际上是运算符,并且在规范中只有几处提到了 precedence 关键字:

  1. https://golang.org/ref/spec#Notation
  2. https://golang.org/ref/spec#Operators

那么,规范的哪个部分会解释所提供表达式的评估顺序?

英文:

Given a simple expression like

*a[0]

where a is declared as *[]string (a pointer to a slice of strings).

Where does the standard exactly explain the order in which the language constructs are evaluated?

I've found that none of them is actually an operator, and the only couple of mentions of the precedence keyword mentioned in the spec:

  1. https://golang.org/ref/spec#Notation
  2. https://golang.org/ref/spec#Operators

So, what part of the spec would explain the order of evaluation of the provided expression?

答案1

得分: 5

确定的部分是Primary Expressions

主要表达式是一元和二元表达式的操作数。

它继续定义了主要表达式,但基本上包括切片表达式,这意味着切片表达式a[0]是一元运算符*的操作数。对于指向数组的指针,有一个特殊情况(见下文)。

根据Address Operators

对于类型为T的操作数x,地址操作&x生成类型为*T的指向x的指针。操作数必须是可寻址的,即变量、指针间接引用或切片索引操作;或可寻址结构操作数的字段选择器;或可寻址数组的数组索引操作。作为对可寻址要求的例外,x也可以是(可能带括号的)复合字面量。如果对x的求值会导致运行时恐慌,则对&x的求值也会导致恐慌。

对于指针类型*T的操作数x,指针间接引用*x表示由x指向的类型为T的变量。如果x为nil,则尝试评估*x将导致运行时恐慌。

这意味着,但没有明确说明,在指针间接引用运算符右侧的切片索引表达式或字段选择器表达式在间接引用之前作为整体进行求值。

另外,指针间接引用(*x一个运算符,具体来说,是一个Address Operator。切片索引引用不是一个运算符,而是一个Index Expression

还要注意:

对于指向数组类型的a:

a[x](*a)[x]的简写

但对于指向切片类型的指针则不适用。

英文:

The definitive section is Primary Expressions:

> Primary expressions are the operands for unary and binary expressions.

It goes on to define primary expressions, but basically, this includes slice expressions, meaning that the slice expression a[0] is the operand for the unary operator *. A special case was made for pointers to arrays (see below).

According to Address Operators:

> For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal. If the evaluation of x would cause a run-time panic, then the evaluation of &x does too.
>
> For an operand x of pointer type *T, the pointer indirection *x denotes the variable of type T pointed to by x. If x is nil, an attempt to evaluate *x will cause a run-time panic.

This implies, but does not explicitly state, that a slice indexing expression or field selector expression to the right of the pointer indirection operator is evaluated as a whole before the indirection is evaluated.

Also, pointer indirection (*x) is an operator, specifically, an Address Operator. The slice index reference is not an operator but rather an Index Expression.

Also note that:

>For a of pointer to array type:
>
> a[x] is shorthand for (*a)[x]

Though the same cannot be said of pointers to slice types.

huangapple
  • 本文由 发表于 2017年7月20日 04:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/45200429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定