Martin Wells said:
user923005:
I don't agree entirely. If:
a) Execution speed is of prime prime prime importance.
b) The value will never be greater than 65536.
, then I wouldn't call it "defective". However I'd use casts wherever
applicable in order to suppress compiler warnings.
Using casts to suppress compiler warnings is widely considered to be a
bad idea. There can be cases, I suppose, where it's necessary, but
what you're really doing is saying to the compiler, "I know exactly
what I'm doing; if you think I'm wrong, please don't tell me". That's
fine if you're right; the problem is when you're wrong (it happens to
all of us), and you've gagged the compiler so it can't tell you.
A lot of newbie C programmers will write something like:
int *ptr = malloc(100 * sizeof(int));
and then change it to
int *ptr = (int*)malloc(100 * sizeof(int));
because the compiler warned them about a type mismatch on the
initialization. In fact, the compiler's warning was correct, and the
cast merely hides it. The programmer was making one of two mistakes:
forgetting the required '#include <stdlib.h>', making the compiler
assume that malloc() returns int, or using a C++ compiler which
doesn't allow this particular implicit conversion.
I'm not saying that you'd necessarily make this particular mistake.
But adding a cast to silence a warning should be thought of as a
fairly drastic step, and it should be very carefully considered.