You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
good rules to follow, but you might like to add a remark about computing the difference between two sizes, because even when using auto you might get unexpected behaviour, ie. value underrun:
std::size_t a=3, b = 5;
auto c = a-b;
c is now some huge number, because c is still unsigned, and you don't get a warning. Also worth noting is that because std::size_t is generally defined as the largest unsigned integer type on a system, generally there is no signed type that will hold all values of a std::size_t. Thus the only safe way of computing the difference requires that you compare b<=a before you compute the difference and take appropriate action if b>a.
The text was updated successfully, but these errors were encountered:
https://github.com/lefticus/cppbestpractices/blob/master/03-Style.md#use-the-correct-integer-type-for-standard-library-features
good rules to follow, but you might like to add a remark about computing the difference between two sizes, because even when using auto you might get unexpected behaviour, ie. value underrun:
c
is now some huge number, becausec
is still unsigned, and you don't get a warning. Also worth noting is that becausestd::size_t
is generally defined as the largest unsigned integer type on a system, generally there is no signed type that will hold all values of astd::size_t
. Thus the only safe way of computing the difference requires that you compareb<=a
before you compute the difference and take appropriate action ifb>a
.The text was updated successfully, but these errors were encountered: