83. Use a checked STL implementation

Summary

Safety first (see Item 6): Use a checked STL implementation, even if it’s only available for one of your compiler platforms, and even if it’s only used during pre-release testing.

Discussion

Just like pointer mistakes, iterator mistakes are far too easy to make and will usually silently compile but then crash (at best) or appear to work (at worst). Even though your compiler doesn’t catch the mistakes, you don’t have to rely on “correction by visual inspection,” and shouldn’t: Tools exist. Use them.

Some STL mistakes are distressingly common even for experienced programmers:

Using an invalidated or uninitialized iterator: The former in particular is easy to do.

Passing an out-of-bounds index: ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.