When dealing with length, prefer std::size_t
. This is an unsigned integer type that is also the return type of the sizeof
operator. It is not good to use int
because you cannot guarantee that any length will fit. Your code will break if the user constructs an object that is too large. There's also this issuethis issue. Accordingly, your loop counter type throughout the class should be std::size_t
.
CORRECTION: @LokiAstari has pointed out that this is now wrong, according to Bjarne Stroustrup (the creator of C++) and other top C++ experts.
The main consensuses here are that:
- mismatching
signed
/unsigned
is a source of bugs - prefer
signed
unless the extra bit is needed for larger values - the STL was wrong about this all along
More information can be found here: