Is there a readable implementation of the STL?

Viewed 16817

I'm on Linux; looking at the STL headers; they're really really complicated.

Is there, somewhere, a smaller version of STL that has the core features of the STL, but is actually readable?

Thanks!

9 Answers

Note that GCC's STL headers have the tab stop set to eight. Reconfigure your editor or replace tabs with eight spaces and it should be much more readable.

Two key points stand out:

  1. No implementation of the STL is readable without an understanding of the goals, rationale, benefits and limitations of the language itself, and general approach.
  2. Most implementations are readable once you have a deep understanding of (1) because the code is self-documenting on those premises. You might not like the formatting, but that really should be the least of your problems.

As a side note, you might have more success with the MSVC version because it's not trying to target multiple as many compilers. Compiler bugs and implementation-defined behavior result in various subtle workarounds. As those workarounds grow in number (as certainly happens when you add more compilers), the code can get gross fast.

RDESTL provides a 'small subset of STL functionality' (but also has some extras). I personally found the code quite instructive and easier to navigate than the big guys like STLPort or Dinkumware's implementation that ships with VC++.

For a more recent and thorough explanation of the "rules" of STL (such as iterators), check out a new book co-authored by Stepanov: http://www.elementsofprogramming.com/

If you enjoy mathematics, this book will excite you, because what the authors describe is essentially an algebra of computation. The site includes a sample chapter.

Well, the STL is pretty complicated, so I think there's a certain amount of essential complexity going on here. It's not surprising that it may seem a little bewildering at first glance.

That said, maybe you could check out Borland's STLport and see if you find that an easier read.

The STL is a highly optimized library that does most of what it does by cleverly exploiting advanced features of C++ and the underlying compiler. Additionally, many things are inlined so there is no real bunch of code to look at like in an application. I'd recommend following Neil's advice.

Related