为什么大多数STL实现中的代码如此复杂? [英] Why is the code in most STL implementations so convoluted?

查看:66
本文介绍了为什么大多数STL实现中的代码如此复杂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

STL是C ++世界的重要组成部分,大多数实现源自Stepanov和Musser的初步努力.

The STL is a critical piece of the C++ world, most implementations derive from the initial efforts by Stepanov and Musser.

我的问题是代码的关键性,它是人们出于敬畏和学习目的而查看编写良好的C ++示例的主要来源之一:为什么STL的各种实现如此令人讨厌地看一下-从美学的角度讲如何不编写C ++代码的复杂且总体上很好的例子.

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.

下面的代码示例在我工作过的地方不会通过代码审查,原因是变量命名,布局,宏以及使用运算符等各种原因不仅仅需要简单了解一下实际情况.

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.

template<class _BidIt> inline
bool _Next_permutation(_BidIt _First, _BidIt _Last)
{  // permute and test for pure ascending, using operator<
_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 < _Zero)
      _Prev += (_Ty)_Mx;
   return (_Prev);
   }

请注意,我并没有批评该界面,因为它的设计和适用性非常好.我担心的是实现细节的可读性.

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.

之前已经提出过类似的问题:

A similar questions have been previously posed:

STL是否具有可读性实现

Is there a readable implementation of the STL

为什么STL的实现如此难以理解?在这里如何改善C ++?

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

注意:上面提供的代码摘自MSVC 2010算法和队列头.

Note: The code presented above is taken from MSVC 2010 algorithm and queue headers.

推荐答案

关于变量名称,库实现者必须使用疯狂的"命名约定,例如以下划线开头,后跟大写字母的名称,因为此类名称是保留名称.为他们.它们不能使用普通"名称,因为这些名称可能已由用户宏重新定义.

About the variables names, library implementors must use "crazy" naming conventions, such as names starting with an underscore followed by an uppercase letter, because such names are reserved for them. They cannot use "normal" names, because those may have been redefined by a user macro.

第17.6.3.3.2节全局名称"§1规定:

Section 17.6.3.3.2 "Global names" §1 states:

某些名称和函数签名的集合始终保留给实现:

Certain sets of names and function signatures are always reserved to the implementation:

  • 每个包含双下划线或以下划线开头且后跟大写字母的名称将保留给实现以供任何使用.

  • Each name that contains a double underscore or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.

以下划线开头的每个名称都保留给实现,以用作全局命名空间中的名称.

Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

(请注意,这些规则禁止像我经常看到的 __ MY_FILE_H 这样的标头防护.)

(Note that these rules forbid header guards like __MY_FILE_H which I have seen quite often.)

这篇关于为什么大多数STL实现中的代码如此复杂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆