0

Reading C++ source code can obviously be a good way to learn and is just plain interesting when its a feature you know very little about. As far as standard library source goes this is difficult due to the uglifying of names.

Is the uglifying done through some sort of post-processing of the source code before it's inclusion in the standard library? If so, what happens to the original source code? Are there any repositories of non-uglified C++ Standard Library source code?

I'm interesting in looking at C++11 and soon C++14 features in particular.

I can't imagine maintaining code in this form would be very fun....

EDIT:

There's a proposal to do away with this process here.

4

1 回答 1

7

在学习标准库实现时,请首先注意标准库必须遵守某些不适用于普通用户编写的代码的严格约束。此外,标准 C++ 库完成的一些操作有些深奥,如果普通用户的代码曾经使用过它们,我会担心(如果您的公司中有一个低级库团队,他们可能会做一些类似的事情,不过)。特别是在约束标准库需要遵守的是,所使用的名称都不会与用户在任何地方使用的任何名称冲突。由于用户可以定义有趣的宏,标准库需要在用户可见的上下文中使用为标准库保留的名称,例如,头文件:

  • 标准 C++ 库中用于类、函数、枚举等的名称或其模板(如果适用)被保留。
  • 在所有上下文中保留所有以下划线开头后跟大写字母的名称或包含两个连续下划线的名称。
  • 但是,还有一些其他保留名称通常不在标头中使用,因为它们大多不保留在宏名称的上下文中。

您特别不想从标准 C++ 库中复制的内容是包含保护的命名:它们通常以一两个下划线开头,并且都是大写字符。用户不得使用这些标识符中的任何一个,除非他们被明确允许使用它们(例如,您可以使用__FILE____LINE__不允许,例如,在您的程序中编写_FOOBAR可以通过宏扩展的任何位置(即,我认为你可以写"_FOOBAR")。

要回答您的实际问题:我知道的所有标准 C++ 库实现(libstdc++、libc++、Dinkumware、RougeWave 和我自己的)都是使用“丑陋的”名称编写的,并且没有程序将它们转换为这种形式。基本上,任何程序都必须处理许多不太可行的异常。我认为,标准 C++ 库的实现者只是习惯了这些有趣的名字。对于我的实现,我努力用一个通用前缀命名所有内容,以便可以使用简单的sed脚本撤消丑化,但我并没有在任何地方都遵循这种模式。

于 2013-10-05T19:38:52.397 回答