Defined bahavior or otherwise?

N

Nick Landsberg

If the following construct defined behaviour
in the standard?

v += v + x;

(Assuming all variables are of the
same type.)

I know what I would expect the construct
to do, but expected is not necessarily
defined.
 
L

Leor Zolman

If the following construct defined behaviour
in the standard?

v += v + x;

Since the result cannot vary due to permissible variations in evaluation
order between sequence points (of which there don't seem to be any here),
you should be all set.
(Assuming all variables are of the
same type.)

I'm not sure it would make any difference if they weren't.
-leor
 
N

Nick Landsberg

Leor said:
Since the result cannot vary due to permissible variations in evaluation
order between sequence points (of which there don't seem to be any here),
you should be all set.



I'm not sure it would make any difference if they weren't.
-leor
Thanks, Leor :)
 
C

Christopher Benson-Manica

Leor Zolman said:
I'm not sure it would make any difference if they weren't.

Well, if one of them is an unsigned short, and the other isn't, then
differences are likely to ensue :)
 
L

Leor Zolman

Well, if one of them is an unsigned short, and the other isn't, then
differences are likely to ensue :)

Well, okay...but as I read it, that would result in
implementation-/defined/ behavior, which would at least remain in the
"defined" category, and thus within the target range of the OP's question
;-)
-leor
 
J

Jack Klein

If the following construct defined behaviour
in the standard?

v += v + x;

(Assuming all variables are of the
same type.)

I know what I would expect the construct
to do, but expected is not necessarily
defined.

If all the variables are of signed or floating point type, there is
always the possibility of undefined behavior if the sum of v and x is
outside the range of the type.

But overflow/underflow aside, the expression if perfectly well
defined. v is modified only once, on the left hand side of the
assignment operator, and the other access to it, on the right hand
side, is used in calculating the new value.
 
B

Barry Schwarz

If the following construct defined behaviour
in the standard?

v += v + x;

(Assuming all variables are of the
same type.)

I know what I would expect the construct
to do, but expected is not necessarily
defined.

Since this is syntactic sugar for
v = v+v+x;
the behavior of both must be equally defined. Other than pathological
cases of underflow or overflow, why would the question even be raised?


<<Remove the del for email>>
 
J

Joona I Palaste

Since this is syntactic sugar for
v = v+v+x;
the behavior of both must be equally defined. Other than pathological
cases of underflow or overflow, why would the question even be raised?

If evaluating v has side effects, the two forms are not equivalent, and
so can hardly be called syntactic sugar.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
C

Christian Bau

Nick Landsberg said:
If the following construct defined behaviour
in the standard?

v += v + x;

Yes. The two exceptions that cause undefined behavior are:

1. Same object modified twice, but v is modified once only.
2. Object modified, and its value is used, but not to determine the
new value. This is not the case, because the old value of v _is_ used to
determine the new value.
 
N

Nick Landsberg

Jack said:
If all the variables are of signed or floating point type, there is
always the possibility of undefined behavior if the sum of v and x is
outside the range of the type.

But overflow/underflow aside, the expression if perfectly well
defined. v is modified only once, on the left hand side of the
assignment operator, and the other access to it, on the right hand
side, is used in calculating the new value.
Thanks, again.
 
D

Dan Pop

IMHO, such behaviour is far more obnoxious than a TIA at the end of the
initial post.

Dan
 
L

Leor Zolman

IMHO, such behaviour is far more obnoxious than a TIA at the end of the
initial post.

If what you're referring to as obnoxious are posts by OPs to say "Thank
you" to their responders, then I'd enthusiastically welcome two instances
of /that/ sort of obnoxiousness for every one of all the other types ;-)
-leor
 
N

Nick Landsberg

Leor said:
If what you're referring to as obnoxious are posts by OPs to say "Thank
you" to their responders, then I'd enthusiastically welcome two instances
of /that/ sort of obnoxiousness for every one of all the other types ;-)
-leor

Actually, I owe folks an apology about that.

I replied to the initial response with a thanks,
not realizing that there were other additional
reponses. (I should have scanned the headers.)
After the second one, I should have realized
that there may be others and just limited
it to a single "thanks to all who replied"
rather than "spamming" the NG.

Mea Culpa.
 
M

Martijn

Nick said:
If the following construct defined behaviour
in the standard?

v += v + x;

This seems a lot trickier:

v = v += x;

;)

I reckon this causes undefined behaviour...
 
P

Peter Pichler

Nick Landsberg said:
If the following construct defined behaviour
in the standard?

v += v + x;

(Assuming all variables are of the
same type.)

If all variablkes are of the same /pointer/ type, then the behaviour is
defined absolutely clearly :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top