 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alessandro Molina Guest
|
Posted: Wed Jan 14, 2004 11:08 pm Post subject: Evaluation precedence questions |
|
|
Standard asserts that there isn't an official way to evaluate
statements like:
a[i] = i++
and similars
I was just curious about the reasons. Why didn't the standard
define a generic evaluation direction for expressions?
What problems will happen stating that compilers must always evaluate
expressions from left or from right for example?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Thu Jan 15, 2004 10:30 am Post subject: Re: Evaluation precedence questions |
|
|
Alessandro Molina wrote:
| Quote: | I was just curious about the reasons. Why didn't the standard
define a generic evaluation direction for expressions?
|
Because We've Always Done It This Way (tm)
People will blather on about optimization possibilities,
but that's a crock. It would be completely reasonable to
adopt Java's order of evaluation semantics and finally
get rid of this stupid albatross. But it will never happen.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu Jan 15, 2004 10:38 am Post subject: Re: Evaluation precedence questions |
|
|
On 14 Jan 2004 18:08:07 -0500, Alessandro Molina <javanx (AT) supereva (DOT) it>
wrote in comp.lang.c++.moderated:
| Quote: | Standard asserts that there isn't an official way to evaluate
statements like:
a[i] = i++
and similars
I was just curious about the reasons. Why didn't the standard
define a generic evaluation direction for expressions?
|
For compatibility with C, and for performance.
| Quote: | What problems will happen stating that compilers must always evaluate
expressions from left or from right for example?
|
What will happen is this:
Any order that you pick will generate efficient object code on some
compilers for some processors, and inefficient object code on some
other compilers for some other processors.
One of the basic tenets of C++ is to be as efficient as C for
low-level programming, and in some cases more efficient than C for
high-level programming.
One of the basic tenets of C, and so inherited by C++, is to be as
efficient as possible. Period.
There are languages that will provide the guarantees that you want.
In general, they are not as fast or efficient as C or C++ at the kind
of low-level programming that C and C++ are used for, especially in
systems programming and real-time embedded systems.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Thu Jan 15, 2004 10:40 am Post subject: Re: Evaluation precedence questions |
|
|
On Wed, 14 Jan 2004 18:08:07 -0500, Alessandro Molina wrote:
| Quote: | Standard asserts that there isn't an official way to evaluate
statements like:
a[i] = i++
and similars
I was just curious about the reasons. Why didn't the standard
define a generic evaluation direction for expressions?
What problems will happen stating that compilers must always evaluate
expressions from left or from right for example?
|
I don't know much about the issues, but I think that the example you have
given above should be legal. Consider:
i = 0;
Then, the quiivalen code generated would be: (possibly!)
a[0] = 0;
++i;
Refards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Jan 15, 2004 3:12 pm Post subject: Re: Evaluation precedence questions |
|
|
In message <gg3c00p4aqehbmvo3bvjd94ande68cnqd9 (AT) 4ax (DOT) com>, Jack Klein
<jackklein (AT) spamcop (DOT) net> writes
| Quote: | On 14 Jan 2004 18:08:07 -0500, Alessandro Molina
wrote in comp.lang.c++.moderated:
Standard asserts that there isn't an official way to evaluate
statements like:
a[i] = i++
and similars
I was just curious about the reasons. Why didn't the standard
define a generic evaluation direction for expressions?
For compatibility with C, and for performance.
|
I recently suggest at a WG14 meeting (C Standard) that future releases
of the C Standard should specify the order of evaluation (it cannot
break any existing well-formed code because the order is currently
unspecified). The only problem is (as I knew when I raised the question)
that it would break some existing compilers.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Fri Jan 16, 2004 2:53 am Post subject: Re: Evaluation precedence questions |
|
|
Jack Klein wrote:
| Quote: | For compatibility with C
|
Of course not, since giving defined behavior to formerly
undefined code cannot cause incompatibility.
and for performance.
In older threads on the same subject people have managed to
come up with isolated cases where God can generate better
code by using an order of evaluation other than "left before
right, operands before operation". The practical effects of
these, especially compared with the decades of erroneous code
that this undefined behavior has engendered, are nil.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Fri Jan 16, 2004 3:01 am Post subject: Re: Evaluation precedence questions |
|
|
On 15 Jan 2004 05:40:21 -0500, "Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote:
| Quote: | On Wed, 14 Jan 2004 18:08:07 -0500, Alessandro Molina wrote:
a[i] = i++
and similars
I don't know much about the issues, but I think that the example you have
given above should be legal. Consider:
|
It is legal, well formed. You just have no idea what it will do.
| Quote: | i = 0;
Then, the quiivalen code generated would be: (possibly!)
a[0] = 0;
++i;
|
or
++i;
a[1] = 0;
or I is a 32 bit thing on an 8 bit machine and the software
increment is interleaved with the subscripting and formats
the hard drive.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Fri Jan 16, 2004 1:07 pm Post subject: Re: Evaluation precedence questions |
|
|
Jack Klein <jackklein (AT) spamcop (DOT) net> wrote
| Quote: | Any order that you pick will generate efficient object code on some
compilers for some processors, and inefficient object code on some
other compilers for some other processors.
|
so rather than pick one, which will be efficient on some platforms,
and not on others, we pick neither, which means that you can't write
the code at all?
joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Fri Jan 16, 2004 1:08 pm Post subject: Re: Evaluation precedence questions |
|
|
Hi!
Dhruv wrote:
| Quote: | I don't know much about the issues, but I think that the example you have
given above should be legal. Consider:
|
It is not legal if i is a built in type (int, long, ...).
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Jan 17, 2004 1:25 am Post subject: Re: Evaluation precedence questions |
|
|
In message <ngdd00p1nmcbhg3kb3p7bvb8grvksjgfkq (AT) 4ax (DOT) com>, John Potter
<jpotter (AT) falcon (DOT) lhup.edu> writes
| Quote: | On 15 Jan 2004 05:40:21 -0500, "Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote:
On Wed, 14 Jan 2004 18:08:07 -0500, Alessandro Molina wrote:
a[i] = i++
and similars
I don't know much about the issues, but I think that the example you have
given above should be legal. Consider:
|
It rather depends on what is meant by 'legal'. It breaches a requirement
of the Standard (no diagnostic required) in that it evaluates i for two
distinct purposes only one of which is to determine the value to be
written (to i)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Jan 17, 2004 1:26 am Post subject: Re: Evaluation precedence questions |
|
|
In message <1074174627.75607 (AT) master (DOT) nyc.kbcfp.com>, Hyman Rosen
<hyrosen (AT) mail (DOT) com> writes
| Quote: | Jack Klein wrote:
For compatibility with C
Of course not, since giving defined behavior to formerly
undefined code cannot cause incompatibility.
|
Careful about the terminology, undefined is usually applied to code
where the Standard says nothing at all and so it can do anything the
compiler decides. Order of evaluation is 'unspecified' which means that
the compiler is strongly constrained and cannot do anything, only choose
some order of evaluation that will deliver the values of operands in a
timely fashion.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Jan 17, 2004 1:32 am Post subject: Re: Evaluation precedence questions |
|
|
Hyman Rosen wrote:
| Quote: | Jack Klein wrote:
For compatibility with C
Of course not, since giving defined behavior to formerly
undefined code cannot cause incompatibility.
and for performance.
In older threads on the same subject people have managed to
come up with isolated cases where God can generate better
code by using an order of evaluation other than "left before
right, operands before operation".
snip |
Nonsense. If the implementation is free to evaluate operands in any
order it can use this freedom to minimise the number of registers or
stack slots used and thus to reduce memory traffic. This is a fairly
basic optimisation that was covered in the Compilers and Operating
Systems course I took as an undergraduate.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Jan 17, 2004 1:33 am Post subject: Re: Evaluation precedence questions |
|
|
In message <31c49f0d.0401151233.3c6de912 (AT) posting (DOT) google.com>, Joshua
Lehrer <usenet_cpp (AT) lehrerfamily (DOT) com> writes
| Quote: | Jack Klein <jackklein (AT) spamcop (DOT) net> wrote
Any order that you pick will generate efficient object code on some
compilers for some processors, and inefficient object code on some
other compilers for some other processors.
so rather than pick one, which will be efficient on some platforms,
and not on others, we pick neither, which means that you can't write
the code at all?
|
How does that follow? By making some things unspecified we turn them
into QoI issues.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Sat Jan 17, 2004 3:13 am Post subject: Re: Evaluation precedence questions |
|
|
Frank Birbacher wrote:
| Quote: | It is not legal if i is a built in type (int, long, ...).
|
And so we spin this stupid thread again and again and again...
Am I the only one who is sick to death of it?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Sat Jan 17, 2004 3:20 am Post subject: Re: Evaluation precedence questions |
|
|
On Thu, 15 Jan 2004 22:01:03 -0500, John Potter wrote:
| Quote: | On 15 Jan 2004 05:40:21 -0500, "Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote:
On Wed, 14 Jan 2004 18:08:07 -0500, Alessandro Molina wrote:
a[i] = i++
and similars
I don't know much about the issues, but I think that the example you have
given above should be legal. Consider:
It is legal, well formed. You just have no idea what it will do.
|
[...]
| Quote: | ++i;
a[1] = 0;
or I is a 32 bit thing on an 8 bit machine and the software
increment is interleaved with the subscripting and formats
the hard drive.
|
Ok, so let me get a few things straight in my mind:
1. Is = a sequence point?
2. wrt. operator++(int) [post increment] applied to ints (PODS), when
exactly is it said that the increment will happen? Possible options
include:
A) Between the assignment taking place, and the RHS expression being
evaluated.
B) After the value of i has been obtained in the expression, so something
like this would be illegal foo (i, i++) (Which I guess is illegal
currently).
C) After the complete expression has been evaluated. (Which was my initial
assumption).
D) None of the above?
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|