Many moments are incomprehensible, for example this:

#define _IOSskipws 0x0001 #define _IOSunitbuf 0x0002 enum _Fmtflags { // constants for formatting options _Fmtmask = 0xffff, _Fmtzero = 0}; static const _Fmtflags skipws = (_Fmtflags)_IOSskipws; static const _Fmtflags unitbuf = (_Fmtflags)_IOSunitbuf; 

These are parts of the code from the xiobase file. Why is _IOSunitbuf reduced to _Fmtflags and how does this cast work?
In general, this is one of the questions. Maybe there are articles / books / ... somewhere that would, if not fully, then partially explain the code in the source? Most likely, experienced programmers will understand almost all the code when they first met the STL, because it is unlikely that they have their own ecosystem and they have already seen similar codes in various sources. Maybe someday I will be able to do it with simplicity, but now I would like some kind of documentation (on the source code, and not on what it gives) for dummies or something).

  • 6
    You should not dig into the source, you have to learn a bunch of unnecessary details of a specific implementation. A formal description is enough to work with STL. I would recommend this: < en.cppreference.com/w >, if something is not clear, ask here or on stackoverflow. - VladD
  • 9
    @VladD how is it in the old jokes? - Remember Dati, for it is understood that it is not possible: PORCH is written without a soft sign, and KOH - with a soft one. Then in chemistry class: - Teacher, did you write to Pachem KOH without a soft sign? - This is alkali, idiot! - alexlz
  • @avp, thanks! Convert your last 3 comments back. - strol
  • 3
    Good question. I, too, have always wondered why the STL code is so ugly designed. I would even say obfustsirovan. - PaulD

1 answer 1

@strol , in fact, this code is reduced to 4 macros

 #define _IOSskipws 1 #define _IOSunitbuf 2 #define skipws _IOSskipws #define unitbuf _IOSunitbuf 

all the rest of the letters are not needed for the compiler to argue about possible errors of the programmer in different parts of the code.

This is usually called a concern for the quality of software (or something like that) and write entire books on this topic.

You ask: Why 4, but not 2 macros?

Most likely this is due to a certain inconsistency in the design (in short, just historically ...)

But I'll write right away that the really big plus of enum _Fmtflags is not only that the compiler checks the types, but also the debugger “sees” these names (as opposed to names in #define).

Also - it happened. Personally, I would prefer to improve the debugger (although, of course, one does not interfere with one another).

-

And this is a response to a comment.

 Ясно). А стоит ли в каких-то других исходниках больших библиотек копаться? К примеру в некоторых частях буста. Или там не лучше с читабельностью? 

@strol , I think no better. This does not mean that STL is bad.

The point here is that:

  1. such code does not pursue educational purposes

  2. a good knowledge of the subject is assumed (i.e. what the function does in what situation, and not the knowledge of the functionality by reading the code)

  3. usually the code is multiplatform (it often either clogs it, or forces the developer to make a fairly abstract structure, where the implementation of a specific functionality lies below, and the data links are not always obvious)

  4. anyone can complement to infinity ...

-

But, still read someone else's code (especially well-known libraries).