Skip to content

Consider adding a remark about difference type of size_t #47

@wrigleyster

Description

@wrigleyster

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions