Wrong result with enum

Stanislav Blinov stanislav.blinov at gmail.com
Thu Nov 11 14:52:45 UTC 2021


On Thursday, 11 November 2021 at 09:11:37 UTC, Salih Dincer wrote:

>> Unless explicitly set, default type is int. 10000100000 is 
>> greater than int.max.
> 100001
> ```d
>   enum w = 100_000;
>   size_t b = w * w;
>   // size_t b = 100000 * 100000; // ???
>   assert(b == 10_000_000_000); // Assert Failure
> ```
> The w!(int) is not greater than the b!(size_t)...

That code is
```
size_t b = int(w) * int(w);
```

That is, `multiply two ints and assign result to a size_t`. 
Multiplication of two ints is still an int though, and you can't 
fit ten billion in an int, so that's overflow. It doesn't matter 
that you declare `b` as `size_t` here. Overflow happens before 
that assignment.


More information about the Digitalmars-d-learn mailing list