2.8.12 Operator Precedence

Table 2.12, “D Operator Precedence and Associativity” lists the D rules for operator precedence and associativity. These rules are somewhat complex, but they are necessary to provide precise compatibility with the ANSI C operator precedence rules. The following entries in the following table are in order from highest precedence to lowest precedence.

Table 2.12 D Operator Precedence and Associativity

Operators

Associativity

() [] -> .

Left to right

! ~ ++ -- + - * & (type) sizeof stringof offsetof xlate

Right to left

* / %

Left to right

+ -

Left to right

<< >>

Left to right

< <= > >=

Left to right

== !=

Left to right

&

Left to right

^

Left to right

|

Left to right

&&

Left to right

^^

Left to right

||

Left to right

?:

Right to left

= += -= *= /= %= &= ^= ?= <<= >>=

Right to left

,

Left to right


Several operators listed in the previous table that have not been discussed yet. These operators are described in subsequent chapters. The following table lists several miscellaneous operators that are provided by the D language.

Operators

Description

For More Information

sizeof

Computes the size of an object.

Section 2.12, “Structs and Unions”

offsetof

Computes the offset of a type member.

Section 2.12, “Structs and Unions”

stringof

Converts the operand to a string.

Section 2.11, “DTrace Support for Strings”

xlate

Translates a data type.

Chapter 17, Translators

unary &

Computes the address of an object.

Section 2.10, “Pointers and Scalar Arrays”

unary *

Dereferences a pointer to an object.

Section 2.10, “Pointers and Scalar Arrays”

-> and .

Accesses a member of a structure or union type.

Section 2.12, “Structs and Unions”

The comma (,) operator that is listed in the table is for compatibility with the ANSI C comma operator. It can be used to evaluate a set of expressions in left-to-right order and return the value of the right most expression. This operator is provided strictly for compatibility with C and should generally not be used.

The () entry listed in the table of operator precedence represents a function call. For examples of calls to functions, such as printf and trace, see Chapter 6, Output Formatting. A comma is also used in D to list arguments to functions and to form lists of associative array keys. Note that this comma is not the same as the comma operator and does not guarantee left-to-right evaluation. The D compiler provides no guarantee regarding the order of evaluation of arguments to a function or keys to an associative array. Note that you should be careful of using expressions with interacting side-effects, such as the pair of expressions i and i++, in these contexts.

The [] entry listed in the table of operator precedence represents an array or associative array reference. Examples of associative arrays are presented in Section 2.9.2, “Associative Arrays”. A special kind of associative array, called an aggregation, is described in Chapter 3, Aggregations. The [] operator can also be used to index into fixed-size C arrays as well. See Section 2.10, “Pointers and Scalar Arrays”.