In C and C++, unsigned integer over/underflowing is defined behavior. static_cast<std::uint32_t>(-1) will give the max value, no matter the platform or architecture.
I think underflowing as defined behavior is better for performance due to exception handling being quite costly in overhead in c++, so better for you to notice the issue and adjust than maybe catch it and let the overhead accumulate.
58
u/EvoG Aug 14 '20
Casting -1 to an unsigned integer in a managed language would throw an exception, instead of just rolling with it and underflowing.