title: Why is the code in most STL implementations so convoluted?
body: <p>The STL is a critical piece of the C++ world, most implementations derive from the initial efforts by Stepanov and Musser.</p>
  
  <p>My question is given the criticality of the code, and it being one of the primary sources for people to view examples of well written C++ for both awe and learning purposes: Why are the various implementations of the STL so disgusting to look at - convoluted and generally good examples of how not to write C++ code from an aesthetics point of view.</p>
  
  <p>The code examples below would not pass code-review at the places I've worked at for reasons varying from variable naming, layout, macros and uses of operators that require more than a simple glance to figure out what is actually occurring.</p>
  
  <pre><code>template&lt;class _BidIt&gt; inline
  bool _Next_permutation(_BidIt _First, _BidIt _Last)
  {  // permute and test for pure ascending, using operator&lt;
  _BidIt _Next = _Last;
  if (_First == _Last || _First == --_Next)
     return (false);
  
  for (; ; )
     {  // find rightmost element smaller than successor
     _BidIt _Next1 = _Next;
     if (_DEBUG_LT(*--_Next, *_Next1))
        {  // swap with rightmost element that's smaller, flip suffix
        _BidIt _Mid = _Last;
        for (; !_DEBUG_LT(*_Next, *--_Mid); )
           ;
        _STD iter_swap(_Next, _Mid);
        _STD reverse(_Next1, _Last);
        return (true);
        }
  
     if (_Next == _First)
        {  // pure descending, flip all
        _STD reverse(_First, _Last);
        return (false);
        }
     }
  }
  
  
  _Ty operator()()
     {  // return next value
     static _Ty _Zero = 0;   // to quiet diagnostics
     _Ty _Divisor = (_Ty)_Mx;
  
     _Prev = _Mx ? ((_Ity)_Ax * _Prev + (_Ty)_Cx) % _Divisor
        : ((_Ity)_Ax * _Prev + (_Ty)_Cx);
     if (_Prev &lt; _Zero)
        _Prev += (_Ty)_Mx;
     return (_Prev);
     }
  </code></pre>
  
  <p>Please note I'm not critiquing the interface, as it is very well designed and applicable. What I'm concerned about is the readability of the implementation details.</p>
  
  <p>A similar questions have been previously posed: </p>
  
  <p><strong><a href="https://stackoverflow.com/questions/2127612/is-there-a-readable-implementation-of-the-stl">Is there a readable implementation of the STL</a></strong></p>
  
  <p><strong><a href="https://stackoverflow.com/questions/1460936/why-stl-implementation-is-so-unreadable-how-c-could-have-been-improved-here">Why STL implementation is so unreadable? How C++ could have been improved here?</a></strong> </p>
  
  <p>Note: The code presented above is taken from MSVC 2010 algorithm and queue headers.</p>