Integer overflow and underflow semantics?

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 23 00:39:40 PDT 2014


On 7/21/2014 2:10 PM, Artur Skawina via Digitalmars-d wrote:
> Actually, C/C++ could get away with treating overflow during constant
> folding as an error (or at least emitting a warning) because of the
> lack of CTFE (and no templates in C's case). The code will either
> compile or it won't.
> For D that is not possible -- if an expression is valid at run-time
> then it should be valid at compile-time (and obviously yield the same
> value). Making this aspect of CT evaluation special would make CTFE
> much less useful and add complexity to the language for very little gain.
> Trying to handle just a subset of the problem would make things even
> worse -- /some/ code would not be CTFE-able and /some/ overflows wouldn't
> be caught.
>
>     int f(int a, int b) { return a*b; }
>     enum v = f(100_000, 100_000);

One difficulty with breaking with C rules is we are working with optimizers and 
code generators developed for C. Coming up with different semantics for D may 
cause all sorts of problems.



More information about the Digitalmars-d mailing list