You might remember situations where you were writing the following code:
void some_function(unsigned short param); int foo(); void do_something() { // Some compilers may warn, that int is being converted to // unsigned short and that there is a possibility of loosing // data. some_function(foo()); }
Usually, programmers just ignore such warnings by implicitly casting to the unsigned short
datatype, as demonstrated in the following code snippet:
// Warning suppressed. some_function( static_cast<unsigned short>(foo()) );
But, what if foo()
returns numbers that do not fit into unsigned short
? This leads to hard detectable errors. Such errors may exist in code for years before they get caught and fixed. Take a look at the foo()
definition:
// Returns -1 if error occurred. int foo() { if (some_extremely_rare_condition()) { return -1; } else if (another_extremely_rare_condition()) { return 1000000; ...