bearophile can say "i told you so" (re uint->int implicit conv)

Jonathan M Davis jmdavisProg at gmx.com
Fri Mar 29 12:29:04 PDT 2013


On Friday, March 29, 2013 17:27:10 Minas Mina wrote:
> Consider:
> uint u = ...;
> int x = u;
> 
> Wouldn't a warning be enough?

No. -w makes it so that warnings are errors, so you generally can't make 
anything a warning unless you're willing for it to be treated as an error at 
least some of the time (and a lot of people compile with -w), and this sort of 
thing is _supposed_ to work without a warning - primarily because if it 
doesn't, you're forced to cast all over the place when you're dealing with 
both signed and unsigned types, and the casts actually make your code more 
error-prone, because you could end up casting something other than uint to int 
or int to uint by accident (e.g. long to uint) and end up with bugs due to 
that.

There are definitely cases where it would be nice to warn about conversions 
between signed and unsigned values, but there's a definite cost to it as well, 
so the situation is not at all clear cut.

- Jonathan M Davis


More information about the Digitalmars-d mailing list