Simple assignment expressions 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Simple assignment expressions



For an expression expr of the form w = expr-rhs:

· The definite assignment state of v before expr-rhs is the same as the definite assignment state of v before expr.

· If w is the same variable as v, then the definite assignment state of v after expr is definitely assigned. Otherwise, the definite assignment state of v after expr is the same as the definite assignment state of v after expr-rhs.

5.3.3.24 && expressions

For an expression expr of the form expr-first && expr-second:

· The definite assignment state of v before expr-first is the same as the definite assignment state of v before expr.

· The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-first is either definitely assigned or “definitely assigned after true expression”. Otherwise, it is not definitely assigned.

· The definite assignment state of v after expr is determined by:

o If expr-first is a constant expression with the value false, then the definite assignment state of v after expr is the same as the definite assignment state of v after expr-first.

o Otherwise, if the state of v after expr-first is definitely assigned, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first is “definitely assigned after false expression”, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after true expression”, then the state of v after expr is “definitely assigned after true expression”.

o Otherwise, if the state of v after expr-first is “definitely assigned after false expression”, and the state of v after expr-second is “definitely assigned after false expression”, then the state of v after expr is “definitely assigned after false expression”.

o Otherwise, the state of v after expr is not definitely assigned.

In the example

class A
{
static void F(int x, int y) {
int i;
if (x >= 0 && (i = y) >= 0) {
// i definitely assigned
}
else {
// i not definitely assigned
}
// i not definitely assigned
}
}

the variable i is considered definitely assigned in one of the embedded statements of an if statement but not in the other. In the if statement in method F, the variable i is definitely assigned in the first embedded statement because execution of the expression (i = y) always precedes execution of this embedded statement. In contrast, the variable i is not definitely assigned in the second embedded statement, since x >= 0 might have tested false, resulting in the variable i being unassigned.

5.3.3.25 || expressions

For an expression expr of the form expr-first || expr-second:

· The definite assignment state of v before expr-first is the same as the definite assignment state of v before expr.

· The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-first is either definitely assigned or “definitely assigned after false expression”. Otherwise, it is not definitely assigned.

· The definite assignment statement of v after expr is determined by:

o If expr-first is a constant expression with the value true, then the definite assignment state of v after expr is the same as the definite assignment state of v after expr-first.

o Otherwise, if the state of v after expr-first is definitely assigned, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first is “definitely assigned after true expression”, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after false expression”, then the state of v after expr is “definitely assigned after false expression”.

o Otherwise, if the state of v after expr-first is “definitely assigned after true expression”, and the state of v after expr-second is “definitely assigned after true expression”, then the state of v after expr is “definitely assigned after true expression”.

o Otherwise, the state of v after expr is not definitely assigned.

In the example

class A
{
static void G(int x, int y) {
int i;
if (x >= 0 || (i = y) >= 0) {
// i not definitely assigned
}
else {
// i definitely assigned
}
// i not definitely assigned
}
}

the variable i is considered definitely assigned in one of the embedded statements of an if statement but not in the other. In the if statement in method G, the variable i is definitely assigned in the second embedded statement because execution of the expression (i = y) always precedes execution of this embedded statement. In contrast, the variable i is not definitely assigned in the first embedded statement, since x >= 0 might have tested true, resulting in the variable i being unassigned.

5.3.3.26! expressions

For an expression expr of the form! expr-operand:

· The definite assignment state of v before expr-operand is the same as the definite assignment state of v before expr.

· The definite assignment state of v after expr is determined by:

o If the state of v after expr-operand is definitely assigned, then the state of v after expr is definitely assigned.

o If the state of v after expr-operand is not definitely assigned, then the state of v after expr is not definitely assigned.

o If the state of v after expr-operand is “definitely assigned after false expression”, then the state of v after expr is “definitely assigned after true expression”.

o If the state of v after expr-operand is “definitely assigned after true expression”, then the state of v after expr is “definitely assigned after false expression”.

Expressions

For an expression expr of the form expr-first?? expr-second:

· The definite assignment state of v before expr-first is the same as the definite assignment state of v before expr.

· The definite assignment state of v before expr-second is the same as the definite assignment state of v after expr-first.

· The definite assignment statement of v after expr is determined by:

o If expr-first is a constant expression (§7.19) with value null, then the the state of v after expr is the same as the state of v after expr-second.

· Otherwise, the state of v after expr is the same as the definite assignment state of v after expr-first.

Expressions

For an expression expr of the form expr-cond? expr-true: expr-false:

· The definite assignment state of v before expr-cond is the same as the state of v before expr.

· The definite assignment state of v before expr-true is definitely assigned if and only if one of the following holds:

o expr-cond is a constant expression with the value false

o the state of v after expr-cond is definitely assigned or “definitely assigned after true expression”.

· The definite assignment state of v before expr-false is definitely assigned if and only if one of the following holds:

o expr-cond is a constant expression with the value true

· the state of v after expr-cond is definitely assigned or “definitely assigned after false expression”.

· The definite assignment state of v after expr is determined by:

o If expr-cond is a constant expression (§7.19) with value true then the state of v after expr is the same as the state of v after expr-true.

o Otherwise, if expr-cond is a constant expression (§7.19) with value false then the state of v after expr is the same as the state of v after expr-false.

o Otherwise, if the state of v after expr-true is definitely assigned and the state of v after expr-false is definitely assigned, then the state of v after expr is definitely assigned.

o Otherwise, the state of v after expr is not definitely assigned.

Anonymous functions

For a lambda-expression or anonymous-method-expression expr with a body (either block or expression) body:

· The definite assignment state of an outer variable v before body is the same as the state of v before expr. That is, definite assignment state of outer variables is inherited from the context of the anonymous function.

· The definite assignment state of an outer variable v after expr is the same as the state of v before expr.

The example

delegate bool Filter(int i);

void F() {
int max;

// Error, max is not definitely assigned
Filter f = (int n) => n < max;

max = 5;
DoWork(f);
}

generates a compile-time error since max is not definitely assigned where the anonymous function is declared. The example

delegate void D();

void F() {
int n;
D d = () => { n = 1; };

d();

// Error, n is not definitely assigned
Console.WriteLine(n);
}

also generates a compile-time error since the assignment to n in the anonymous function has no affect on the definite assignment state of n outside the anonymous function.

 

Variable references

A variable-reference is an expression that is classified as a variable. A variable-reference denotes a storage location that can be accessed both to fetch the current value and to store a new value.

variable-reference:
expression

In C and C++, a variable-reference is known as an lvalue.



Поделиться:


Последнее изменение этой страницы: 2016-08-10; просмотров: 226; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 13.59.62.219 (0.012 с.)