D programming Language



Download 1.66 Mb.
Page9/47
Date08.01.2017
Size1.66 Mb.
#7507
1   ...   5   6   7   8   9   10   11   12   ...   47

Evaluation Order


Unless otherwise specified, the implementation is free to evaluate the components of an expression in any order. It is an error to depend on order of evaluation when it is not specified. For example, the following are illegal:

i = ++i;

c = a + (a = b);

func(++i, ++i);

If the compiler can determine that the result of an expression is illegally dependent on the order of evaluation, it can issue an error (but is not required to). The ability to detect these kinds of errors is a quality of implementation issue.

Expressions


AssignExpression , Expression

The left operand of the , is evaluated, then the right operand is evaluated. The type of the expression is the type of the right operand, and the result is the result of the right operand.


Assign Expressions


ConditionalExpression = AssignExpression

The right operand is implicitly converted to the type of the left operand, and assigned to it. The result type is the type of the lvalue, and the result value is the value of the lvalue after the assignment.

The left operand must be an lvalue.

Assignment Operator Expressions


ConditionalExpression += AssignExpression

ConditionalExpression -= AssignExpression

ConditionalExpression *= AssignExpression

ConditionalExpression /= AssignExpression

ConditionalExpression %= AssignExpression

ConditionalExpression &= AssignExpression

ConditionalExpression |= AssignExpression

ConditionalExpression ^= AssignExpression

ConditionalExpression <<= AssignExpression

ConditionalExpression >>= AssignExpression

ConditionalExpression >>>= AssignExpression

Assignment operator expressions, such as:



a op= b

are semantically equivalent to:



a = a op b

except that operand a is only evaluated once.


Conditional Expressions


OrOrExpression ? Expression : ConditionalExpression

The first expression is converted to bool, and is evaluated. If it is true, then the second expression is evaluated, and its result is the result of the conditional expression. If it is false, then the third expression is evaluated, and its result is the result of the conditional expression. If either the second or third expressions are of type void, then the resulting type is void. Otherwise, the second and third expressions are implicitly converted to a common type which becomes the result type of the conditional expression.


OrOr Expressions


AndAndExpression || AndAndExpression

The result type of an OrOr expression is bool, unless the right operand has type void, when the result is type void.

The OrOr expression evaluates its left operand. If the left operand, converted to type bool, evaluates to true, then the right operand is not evaluated. If the result type of the OrOr expression is bool then the result of the expression is true. If the left operand is false, then the right operand is evaluated. If the result type of the OrOr expression is bool then the result of the expression is the right operand converted to type bool.

AndAnd Expressions


OrExpression && OrExpression

The result type of an AndAnd expression is bool, unless the right operand has type void, when the result is type void.

The AndAnd expression evaluates its left operand. If the left operand, converted to type bool, evaluates to false, then the right operand is not evaluated. If the result type of the AndAnd expression is bool then the result of the expression is false. If the left operand is true, then the right operand is evaluated. If the result type of the AndAnd expression is bool then the result of the expression is the right operand converted to type bool.

Bitwise Expressions


Bit wise expressions perform a bitwise operation on their operands. Their operands must be integral types. First, the default integral promotions are done. Then, the bitwise operation is done.

Or Expressions


XorExpression | XorExpression

The operands are OR'd together.


Xor Expressions


AndExpression ^ AndExpression

The operands are XOR'd together.


And Expressions


EqualExpression & EqualExpression

The operands are AND'd together.


Equality Expressions


RelExpression == RelExpression

RelExpression != RelExpression

Equality expressions compare the two operands for equality (==) or inequality (!=). The type of the result is bool. The operands go through the usual conversions to bring them to a common type before comparison.

If they are integral values or pointers, equality is defined as the bit pattern of the type matches exactly. Equality for struct objects means the bit patterns of the objects match exactly (the existence of alignment holes in the objects is accounted for, usually by setting them all to 0 upon initialization). Equality for floating point types is more complicated. -0 and +0 compare as equal. If either or both operands are NAN, then both the == and != comparisons return false. Otherwise, the bit patterns are compared for equality.

For complex numbers, equality is defined as equivalent to:

x.re == y.re && x.im == y.im

and inequality is defined as equivalent to:

x.re != y.re || x.im != y.im

For class objects, equality is defined as the result of calling Object.eq(). Two null objects compare as equal, if only one is null they compare not equal.

For static and dynamic arrays, equality is defined as the lengths of the arrays matching, and all the elements are equal.



Download 1.66 Mb.

Share with your friends:
1   ...   5   6   7   8   9   10   11   12   ...   47




The database is protected by copyright ©ininet.org 2024
send message

    Main page