I freak out whenever I open up any STL-related code from Visual Studio’s implementation while debugging my code:
// From <xtree>
if (_Where == begin())
{ // insert at beginning if before first element
if (_DEBUG_LT_PRED(this->comp,
this->_Kfn(_Val), _Key(_Where._Mynode())))
return (_Insert(true, _Where._Mynode(), _Val));
}
else if (_Where == end())
{ // insert at end if after last element
if (_DEBUG_LT_PRED(this->comp,
_Key(_Rmost()), this->_Kfn(_Val)))
return (_Insert(false, _Rmost(), _Val));
}
//...
else if (_DEBUG_LT_PRED(this->comp,
_Key(_Where._Mynode()), this->_Kfn(_Val))
&& (++(_Next = _Where) == end()
|| _DEBUG_LT_PRED(this->comp,
this->_Kfn(_Val), _Key(_Next._Mynode()))))
{ // insert after _Where
if (_Isnil(_Right(_Where._Mynode())))
return (_Insert(false, _Where._Mynode(), _Val));
else
return (_Insert(true, _Next._Mynode(), _Val));
}
The presence of comments makes me feel as though a human wrote them, but the poor formatting, liberal use of underscores at the beginning of everything (why?), and extremely unreadable conditions like (++(_Next = _Where) == end() make me feel as though they were generated from another piece of source code, not written as-is.
|| _DEBUG_LT_PRED ...)
Does anyone know which of those is the case? (If it was generated from some other piece of code, I’d be interested in knowing how/why this was done.)
For the record, here’s the same thing, but “properly formatted”:
if (Where == begin())
{
// insert at beginning if before first element
if (DEBUG_LT_PRED(this->comp, this->Kfn(Val), Key(Where.Mynode())))
return (Insert(true, Where.Mynode(), Val));
}
else if (Where == end())
{
// insert at end if after last element
if (DEBUG_LT_PRED(this->comp, Key(Rmost()), this->Kfn(Val)))
return (Insert(false, Rmost(), Val));
}
//...
else if (DEBUG_LT_PRED(this->comp, Key(Where.Mynode()), this->_Kfn(Val))
&& (++(Next = Where) == end()
|| DEBUG_LT_PRED(this->comp, this->_Kfn(Val), Key(Next.Mynode()))))
{
// insert after Where
if (Isnil(Right(Where.Mynode())))
return (Insert(false, Where.Mynode(), Val));
else
return (Insert(true, Next.Mynode(), Val));
}
IMHO this is more like how it would turn out if a human wrote it, but then again, I have no idea.
Two things:
The indentation is actually fine, although nowadays unusual (and I personally hate it): they use an indentation of four, which is achieved via spaces, but use tabs for all multiples of eight. This used to be the standard almost everywhere (notably it’s still the default setting in several editors such as Vim). But as a consequence, the code is only indented correctly if you set your tab width to 8. So the code actually looks like this:
Which, though still unusual, is perfectly logical and legible.
It’s good style (or even mandated?) that the standard library uses only reserved identifiers to avoid name clashes with customers’ C++ code. These reserved names are either names starting with an underscore followed by a capital letter (
_DEBUG_LT_PRED,_Key), or two underscores (not in this code, but the GCC libstdc++ is littered with__xetc.).Hence the alphabet soup.
But yes, this code is indeed manually written – at least it is in the case of the GCC. The active source branch of the libstdc++ looks exactly like the code above, and isn’t auto-generated.