Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding a remark about difference type of size_t #47

Closed
wrigleyster opened this issue May 20, 2016 · 0 comments
Closed

Consider adding a remark about difference type of size_t #47

wrigleyster opened this issue May 20, 2016 · 0 comments

Comments

@wrigleyster
Copy link

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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant