jacob navia <> writes:
> Le 28/12/10 22:46, Keith Thompson a écrit :
[...]
>> I'm almost tempted to suggest that adding exception handling to
>> the language might not be a completely horrible idea.
>>
> lcc-win proposes:
Can you clarify what you mean by "lcc-win proposes"? If you're
proposing that this should be added to a future C standard, it would
make more sense to say that *you* propose it; if not, I suggest that
"lcc-win implements" would make more sense. (I'm not picking on you,
just trying to understand.)
> int a,b,c;
> // ...
> c = a+b;
> if (_overflow()) {
> }
>
> What's wrong with that?
I won't say there's something *wrong* with it, and it's better
than nothing, but the precise semantics of _overflow() are not
at all obvious. It shares the same problems we already have with
errno, except that it applies to operators with expressions rather
than function calls. For one thing, unlike C++-style exceptions,
it's not scoped. Suppose I write:
x = y * z;
c = a + b;
if (_overflow()) {
...
}
If the multiplication overflows and the addition doesn't, what does
_overflow() return? What if there are multiple operations within
an expression? If each operation clears the overflow flag, undefined
evaluation orders mean that you can't safely use _overflow() unless
you restrict yourself to very simple expressions. And if it's
"sticky", is there a separate operation that clears the flag?
You probably have specific answers to most or all of these questions.
My point is that it's not obvious at the language level what those
answers should be.
I suggest that a mechanism that tells you whether an overflow
occurred anywhere within a specified chunk of code, perhaps
within the execution of a given expression, would be clearer.
Perhaps something like:
if (_overflow(c = a + b)) {
/* an overflow occurred */
}
else {
/* no overflow occurred */
}
Or just C++-like exception handling, for which we have ample existing
practice.
> In i386asm:
> addl %eax,%ecx
> jo _overflowhandler
If this were to be added to the language, its semantics would have
to be defined in terms of actual program behavior, not in terms of
x86 instructions. Not all CPUs have overflow flags that behave
exactly like the x86 overflow flag.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"