Why STL implementation is so unreadable? How C++ could have been improved here?

前端 未结 3 1483
一整个雨季
一整个雨季 2021-01-01 09:01

For instance why does most members in STL implementation have _M_ or _ or __ prefix? Why there is so much boilerplate code ?

W

3条回答
  •  一整个雨季
    2021-01-01 09:30

    In addition to the good reasons robson and AshleysBrain have already given, one reason that C++ standard library implementations have such terse names and compact code is that virtually every C++ program (compilation unit, really) includes a large number of the standard library headers, and they are thus repeatedly recompiled (remember that they're largely inlined and template-based, whereas the C standard library headers only contain a handful of function declarations). A standard library written to "industry standard" style guidelines would take longer to compile and thus lead to the perception that a particular compiler was "slow". By minimizing whitespace and using short identifier names, the lexer and parser have less work to do, and the whole compilation process completes a little bit faster.

    Another reason worth mentioning is that many standard library implementations (e.g. Dinkumware, Rogue Wave (old), etc.) can be used with several different compilers with widely different standards compliance and quirks. There's frequently a lot of macro hackery aimed at satisfying each supported platform.

提交回复
热议问题