T
Taras_96
Hi everyone,
I have a couple of questions about expressions and the subsequent
order of evaluation.
Firstly, as I understand it, an expression is what wiki defines it to
be: "An expression in a programming language is a combination of
values, variables, operators, and functions that are interpreted
(evaluated) according to the particular rules of precedence and of
association for a particular programming language, which computes and
then produces (returns, in a stateful environment) another value".
I am also aware that expressions can be grouped into sub-expressions.
eg: 2 + 3 + 4 can be thought of as two expressions, (2+3) & 5 + 4
According to TCPL (Bjarne Stroustrup), the order of evaluation of sub-
expressions is not defined. Because of this, I'm guessing that how a
compound expression is decomposed into a sub-expression can effect
whether an expression is defined or not. How is a compound expression
decomposed into atomic sub-expressions? Is it according to precedence
& associativity rules?
eg: the addition operator is right-associative. If we have the
expression:
f() + g() + h()
Which, because the addition operator is left associative, can be
written (into 'atomic' sub-expressions) as
( f() + g() ) + h()
Then is it the case that we can't guarantee whether h() gets evaluated
first or second, but *can* we guarantee that IF f() IS evaluated
before h(), then the expression ( f() + g() ) will be evaluated before
h() (ie: sub-expressions get evaluated as a complete unit)?
--------------------------------------------
Bjarne also writes that the expression
int i = 1;
v = v[i++];
is undefined: "may be evaluated as either v [1 ]=1 or v [2 ]=1 or may
cause some even stranger behavior."
This comes as a surprise, as I would have thought that the RHS of an
assignment operator would be evaluated before the LHS!
By the same virtue, does it mean that v = v[++i] is also undefined?
(if my understanding of post/pre fix operators is correct).
Does this mean that the following is also undefined?
class A
{
int a;
public:
int & returnA( return a;}
}
returnA() = 3 + 2;
as we don't know whether returnA() is evaluated first or 3+2 is
evaluated first? I would think the 'logical' choice would be that 3+2
would be evaluated first...
------------------
A c++ expression always evaluates to a value, so x = 3 + 2 is an
expression that actually evaluates to 5... so in y=x=3+2, y get's
assigned the value of (x = 3 + 2), which is 5. Is it always the case
that the evaluated value for a statement involving an assignment
operator is always what both individual sides evaluate to (I assume
that this is the case)? I also guess that this does not hold true for
user defined types, as theoretically you could return anything from
the operator= function
------------------
Finally, at http://tiny.cc/6C2UA, Victor Bazarov states that
expression x[x[0]] = 2 is not conforming "for one simple reason:
you're accessing and changing the value of x[0] in the same
expression."
x[0] = 0;
x[2] = 123;
x[x[0]] = 2;
Could someone explain this a bit further? Going on my previous 'guess'
at precedence, I suppose that something like:
x[0] = (x[0] + 2);
would be defined, but:
x[0] = x[0] + 2
would not
I'm impressed if you've read this far
Taras
I have a couple of questions about expressions and the subsequent
order of evaluation.
Firstly, as I understand it, an expression is what wiki defines it to
be: "An expression in a programming language is a combination of
values, variables, operators, and functions that are interpreted
(evaluated) according to the particular rules of precedence and of
association for a particular programming language, which computes and
then produces (returns, in a stateful environment) another value".
I am also aware that expressions can be grouped into sub-expressions.
eg: 2 + 3 + 4 can be thought of as two expressions, (2+3) & 5 + 4
According to TCPL (Bjarne Stroustrup), the order of evaluation of sub-
expressions is not defined. Because of this, I'm guessing that how a
compound expression is decomposed into a sub-expression can effect
whether an expression is defined or not. How is a compound expression
decomposed into atomic sub-expressions? Is it according to precedence
& associativity rules?
eg: the addition operator is right-associative. If we have the
expression:
f() + g() + h()
Which, because the addition operator is left associative, can be
written (into 'atomic' sub-expressions) as
( f() + g() ) + h()
Then is it the case that we can't guarantee whether h() gets evaluated
first or second, but *can* we guarantee that IF f() IS evaluated
before h(), then the expression ( f() + g() ) will be evaluated before
h() (ie: sub-expressions get evaluated as a complete unit)?
--------------------------------------------
Bjarne also writes that the expression
int i = 1;
v = v[i++];
is undefined: "may be evaluated as either v [1 ]=1 or v [2 ]=1 or may
cause some even stranger behavior."
This comes as a surprise, as I would have thought that the RHS of an
assignment operator would be evaluated before the LHS!
By the same virtue, does it mean that v = v[++i] is also undefined?
(if my understanding of post/pre fix operators is correct).
Does this mean that the following is also undefined?
class A
{
int a;
public:
int & returnA( return a;}
}
returnA() = 3 + 2;
as we don't know whether returnA() is evaluated first or 3+2 is
evaluated first? I would think the 'logical' choice would be that 3+2
would be evaluated first...
------------------
A c++ expression always evaluates to a value, so x = 3 + 2 is an
expression that actually evaluates to 5... so in y=x=3+2, y get's
assigned the value of (x = 3 + 2), which is 5. Is it always the case
that the evaluated value for a statement involving an assignment
operator is always what both individual sides evaluate to (I assume
that this is the case)? I also guess that this does not hold true for
user defined types, as theoretically you could return anything from
the operator= function
------------------
Finally, at http://tiny.cc/6C2UA, Victor Bazarov states that
expression x[x[0]] = 2 is not conforming "for one simple reason:
you're accessing and changing the value of x[0] in the same
expression."
x[0] = 0;
x[2] = 123;
x[x[0]] = 2;
Could someone explain this a bit further? Going on my previous 'guess'
at precedence, I suppose that something like:
x[0] = (x[0] + 2);
would be defined, but:
x[0] = x[0] + 2
would not
I'm impressed if you've read this far
Taras