Re: Need some clarification
On Feb 25, 11:47 am, joshuamaur...@gmail.com wrote:
On Feb 24, 5:25 pm, Hyman Rosen <hyro...@mail.com> wrote:
capricorn wrote:
C++
int k1 = i++ + ++i + j++ + ++j;
Because of a mistaken belief that efficiency of programs
is assisted by ambiguity of programming languages, the
above statement has undefined behavior in C++.
This reminds me a lot of strict aliasing. I am under the impression
strict aliasing allows several powerful and noticeable compiler
optimizations which seem quite similar to the optimizations that could
be done on the above (convoluted) example, or at least code similar to
the above example, like:
int foobar(int & a, int & b, int & c, int & d)
{ return a++ + ++b + c++ + ++d;
}
It's quite simple to see that forcing a particular order of evaluation
of foobar on a compiler is going to produce unoptimal code if a, b, c,
and d don't alias each other. If the compiler is free to assume they
don't alias each other, as is the case in C++,
But a C++ compiler is not free to assume that a, b, c, d refer to four
different ints. Even though the implementation of foobar() makes such
an assumption - a C++ compiler may not. C++ would need the equivalent
of C99's "restrict" keyword before the compiler could assume that none
of foobar's parameters alias another.
So, as written, foobar() is simply buggy. Its interface allows the
caller to pass the same int object for multiple arguments - even
though such aliasing causes undefined behavior inside foobar(). And it
hardly matters how fast foobar() executes when passed one set of
arguments, when its behavior is undefined when passed another set of
arguments.
the compiler can make
assembly to do the loads, stores, and additions in parallel, whereas
in languages like Java, the operations are forced to proceed serially.
Thus I find it silly to argue:
Java does not even have pointers or references, so examples like foobar
() are not even pertinent. Now, Java does have the same "as-if" rule
as C++, so the actual order of evaluation of an expression in Java
depends only on the expression's observed behasvior. Since nothing in
the example program depends on the order in which "j" and "k" are
incremented, it is safe to say that a Java compiler would have just as
much latitude as a C++ compiler in optimizing the expression.
a mistaken belief that efficiency of programs
is assisted by ambiguity of programming languages,
The question is merely one of degrees. How often does (convoluted)
code like foobar occur in practice, or code that benefits from the
unspecified order of evaluation like foobar, and how much savings do
we get? Then one has to compare this speed bonus to the penalty of
undefined behavior?
No C++ code benefits derive from having an undefined order of
evaluation - because no one would intentionally write code with such a
dependency in the first place.So why does a C++ even allow a
programmer to write expressions with multiple increments, if the
behavior of such expressions is undefined? Alternately, why doesn't
the C++ compiler simply do what the C++ programmer is expected to do:
namely, to insert the needed sequence points that would make the
expression well-defined. Either alternative would be far more sensible
than the current laissez-faire approach.
In short, when it comes to order-of evaluation, C++ seems to grossly
underestimate the capabilities of modern C++ compilers, while it
greatly overestimates the willingness of C++ programmers to put up
with this nonsense.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]