2 ms·
You've traded one hazard for another there, now you have a bug at runtime if the container size is between the range of signed int and unsigned int. Hope that c
by mappu 12y ago
You've traded one hazard for another there, now you have a bug at runtime if the container size is between the range of signed int and unsigned int. Hope that container size isn't user controlled.
I'm guilty of a few C-style casts, but certainly prefer catching things at compile time than runtime. So +1 unsigned types for me. At least the compiler helps with catching signedness issues.
- cannam 12y agoDoes the compiler help? $ cat unsigned.cpp #include <iostream> void f(unsigned int x) { std::cout << "x = " << x << std::endl; } int main() { f(-1); } $ g++ -Wall -Wextra -Wconversion -Werror unsigned.cpp $ ./a.out x = 4294967295 No error, no warning. The two are not really distinct types. See the link I posted above for more argumentation along these lines.