11

I am looking at various STL headers provided with compilers and I cant imagine the developers actually writing all this code by hand. All the macros and the weird names of varaibles and classes - they would have to remember all of them! Seems error prone to me. Are parts of the headers result of some text preprocessing or generation?

onqtam
  • 4,356
  • 2
  • 28
  • 50
  • 2
    Can you give an example of one you found? For the most part, the headers will be written by hand. There might be a little bit of code generation sprinkled here and there, but mostly done by the preprocessor. – Joseph Mansfield Feb 19 '14 at 17:58
  • It will become easy if you do a lot of coding. :P – herohuyongtao Feb 19 '14 at 17:59
  • 1
    They are written by hand, how else would you expect them to be done? The headers aren't just written all at once, everything known before they write them. It's a process, adding functionality as it's needed, tweaking, improving, fixing bugs. It's a long process. Frankly if you think the STL headers are bad, you've seen nothing yet. The STL headers are typically well managed and from what I've seen can be very well designed. – leetNightshade Feb 19 '14 at 18:01
  • They were written by hand. If you look near the end of most of Visual Studio STL headers, you'll see that most of them were done by HP, with a copyright date of 1994. There's a committee that decides what should be included in C++ STL. – rcgldr Feb 19 '14 at 18:59

2 Answers2

37

I've maintained Visual Studio's implementation of the C++ Standard Library for 7 years (VC's STL was written by and licensed from P.J. Plauger of Dinkumware back in the mid-90s, and I work with PJP to pick up new features and maintenance bugfixes), and I can tell you that I do all of my editing "by hand" in a plain text editor. None of the STL's headers or sources are automatically generated (although Dinkumware's master sources, which I have never seen, go through automated filtering in order to produce customized drops for Microsoft), and the stuff that's checked into source control is shipped directly to users without any further modification (now, that is; previously we ran them through a filtering step that caused lots of headaches). I am notorious for not using IDEs/autocomplete, although I do use Source Insight to browse the codebase (especially the underlying CRT whose guts I am less familiar with), and I extensively rely on grep. (And of course I use diff tools; my favorite is an internal tool named "odd".) I do engage in very very careful cut-and-paste editing, but for the opposite reason as novices; I do this when I understand the structure of code completely, and I wish to exactly replicate parts of it without accidentally leaving things out. (For example, different containers need very similar machinery to deal with allocators; it should probably be centralized, but in the meantime when I need to fix basic_string I'll verify that vector is correct and then copy its machinery.) I've generated code perhaps twice - once when stamping out the C++14 transparent operator functors that I designed (plus<>, multiplies<>, greater<>, etc. are highly repetitive), and again when implementing/proposing variable templates for type traits (recently voted into the Library Fundamentals Technical Specification, probably destined for C++17). IIRC, I wrote an actual program for the operator functors, while I used sed for the variable templates. The plain text editor that I use (Metapad) has search-and-replace capabilities that are quite useful although weaker than outright regexes; I need stronger tools if I want to replicate chunks of text (e.g. is_same_v = is_same< T >::value).

How do STL maintainers remember all this stuff? It's a full time job. And of course, we're constantly consulting the Standard/Working Paper for the required interfaces and behavior of code. (I recently discovered that I can, with great difficulty, enumerate all 50 US states from memory, but I would surely be unable to enumerate all STL algorithms from memory. However, I have memorized the longest name, as a useless bit of trivia. :->)

Stephan T. Lavavej
  • 1,781
  • 14
  • 8
6

The looks of it are designed to be weird in some sense. The standard library and the code in there needs to avoid conflicts with names used in user programs, including macros and there are almost no restrictions as to what can be in a user program.

They are most probably hand written, and as others have mentioned, if you spend some time looking at them you will figure out what the coding conventions are, how variables are named and so on. One of the few restrictions include that user code cannot use identifiers starting with _ followed by a capital letter or __ (two consecutive underscores), so you will find many names in the standard headers that look like _M_xxx or __yyy and it might surprise at first, but after some time you just ignore the prefix...

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • The C standard, and i guess the C++ is based on this 40 year old decision. IS that one underscore is enough (capital or lower case letter) to make it a runtime defined symbol while 2 underscores were reserved for for compiler defined symbols. – Lothar Feb 15 '16 at 18:59
  • @Lothar: The rules outlined in the answer come directly from the C++ standard. There are other limitations (`_x` is also reserved in the global namespace), that don't apply to this question. – David Rodríguez - dribeas Feb 17 '16 at 09:37