Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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:

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 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:

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 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:

added 603 characters in body; Post Made Community Wiki
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

When dealing with length, prefer std::size_t. This is an unsigned integer typeWhen 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 issue. Accordingly, your loop counter type throughout the class should be std::size_t.

CORRECTION: @LokiAstari has pointed out that is also the return type of the sizeof operator. It is not goodthis is now wrong, according to useBjarne Stroustrup int because you cannot guarantee that any length will fit(the creator of C++) and other top C++ experts. Your code will break if the user constructs an object

The main consensuses here are that is too large. There's also this issue. Accordingly, your loop counter type throughout the class should:

  • 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 std::size_t.found here:

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 issue. Accordingly, your loop counter type throughout the class should be std::size_t.

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 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:

Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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 issue. Accordingly, your loop counter type throughout the class should be std::size_t.