java iostream_为什么 C++ 要搞出类似 iostream 这样的无后缀文件?

C++ 并没有要求存在名为 iostream 的文件。你看到的只是编译器在具体实现时取巧的结果。

C++ 历代标准,在介绍标准库的「Header(头文件)」时,都有大同小异的脚注:A header is not necessarily a source file, nor are the sequences delimited by < and > in header names necessarily valid source file names.

即:Header 不一定要是源文件,尖括号之间的 header 名称序列也不一定要是有效的文件名。

你看,人家都称其为 header 而非 header file。我们始终翻译为「头文件」是习惯使然。

回溯历史,可以在 N0119 文档(1991 年的标准委员会 Library 组会议纪要)里找到源头:The group has discussed the header "file" naming convention used in the standard library. Existing C++ implementations use a variety of header file naming conventions (.h, .hxx, .hpp, .H, to name a few). The lack of consistency creates definite problems of portability and library usage for C++ programmers. Settling upon a consistent convention in the standard would encourage C++ implementations to adopt a consistent scheme.

A new aspect of the discussion is the opportunity to differentiate C++ headers from C headers. For example, string.h could refer to the existing C str* functions, while a string header could refer to the C++ string classes. However, changing the name of iostream.h to iostream would impact existing C++ implementations of iostreams.

可见当时 C++ 标准库头文件异常混乱,.h / .hxx / .hpp / .H 用什么的都有,于是开始考虑制定统一标准,同时希望可以和已有的 C 标准库头文件存在明显区分,比如有人就提议:#include 引入 C 的字符串头文件

#include 引入 C++ 的字符串头文件

与此同时,人们也开始考虑是不是要把原有的 iostream.h 也改成 iostream,同时衡量其影响。

当然此时考虑的还是「头文件就是文件」思路,直到 1992 年 11 月的又一次报告 N0208 中,关于头文件的命名才得以确定:Header "file" names will have no trailing suffix. The mapping of these names onto file names is, as ever, implementation dependent.

即决定:标准库「头『文件』的名称」不使用后缀,如何将这些「名称」映射到具体的文件名,由实现决定。

所以从标准库制定头文件命名规范伊始,尖括号之间的东西就只是「名称」而非「文件名」,#include 完全可以引入一个叫 biaozhunliu.hpp 的文件。 而根据后续正式标准文档,甚至允许不存在任何实体文件,直接替换内容。编译器实现时选择使用一个叫 iostream 的无扩展名文件,是自己取巧的结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值