包括文件排序策略
我看到了相当一致的建议,即实现文件(.cc / .cpp)应包含其相应的类 首先定义文件,然后再包含其他头文件。但是,当主题转向头文件本身及其包含的包含顺序时,建议似乎有所不同。
Google 编码标准建议:
- dir2/foo2 .h(首选位置 - 请参阅下面的详细信息)。
- C 系统文件。
- C++ 系统文件。
- 其他库的 .h 文件。
- 您项目的 .h 文件。
目前尚不清楚上述条目 1 和条目 5 之间有何区别,以及为何选择其中一个或另一个位置。也就是说,另一个在线指南建议了这个顺序(可以在“类布局”部分找到)该文档):
- 系统包含
- 项目包含
- 本地包含
再次出现歧义,这次是在第 2 项和第 3 项之间。有什么区别?这些是否代表项目间和项目内包含?
但更重要的是,看起来两个提议的编码标准都建议最后包含“您的”头文件。这种建议与实现文件中包含排序的建议相反,并不直观。将“您的”头文件始终列在前面 - 在系统头文件和第 3 方头文件之前没有意义吗?
I've seen fairly consistent advice that an implementation file (.cc / .cpp) should include its corresponding class definition file first, before including other header files. But when the topic shifts to header files themselves, and the order of includes they contain, the advice seems to vary.
Google coding standards suggest:
- dir2/foo2.h (preferred location — see details below).
- C system files.
- C++ system files.
- Other libraries' .h files.
- Your project's .h files.
It is unclear what the difference is between entry 1 and 5 above, and why one or the other location would be chosen. That said, another online guide suggests this order (found in the "Class Layout" section of that doc):
- system includes
- project includes
- local includes
Once again there is an ambiguity, this time between items 2 and 3. What is the distinction? Do those represent inter-project and intra-project includes?
But more to the point, it looks as if both proposed coding standards are suggesting "your" header files are included last. Such advice, being backwards from what is recommended for include-ordering in implementation files, is not intuitive. Would it not make sense to have "your" header files consistently listed first - ahead of system and 3rd party headers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从技术角度来看,您列出的内容的顺序并不重要。如果设计正确,您应该能够按照您想要的任何顺序放置它们,并且它仍然可以工作。例如,如果您的
foo.h
需要
,它应该包含在您的foo.h
中,这样您就不需要在任何使用foo
的地方都要记住这种依赖关系。话虽这么说,如果您确实有顺序依赖性,大多数时候将定义文件放在最后会修复它。这是因为
foo.h
依赖于
,但反之则不然。您可能认为这是将定义文件放在最后的一个很好的例子,但实际上恰恰相反。如果您的编码标准要求首先定义,那么您的编译器更有可能在首次编写时捕获不正确的顺序依赖项。
The order you list your includes shouldn't matter from a technical point of view. If you designed it right, you should be able to put them in any order you want and it will still work. For example, if your
foo.h
needs<string>
, it should be included inside yourfoo.h
so you don't have to remember that dependency everywhere you usefoo
.That being said, if you do have order dependencies, most of the time putting your definition file last will fix it. That's because
foo.h
depends on<string>
, but not the other way around.You might think that makes a good case for putting your definition file last, but it's actually quite the opposite. If your coding standards require the definition first, your compiler is more likely to catch incorrect order dependencies when they are first written.
我不知道有任何逐字标准,但作为一般经验法则,应包含尽可能少的标头,尤其是在其他标头文件中,以减少编译时间、冲突和依赖性。我喜欢在头文件中使用类的前向声明,并且只要我有能力,就只在 .cpp 端包含头文件和定义。
也就是说,我个人的偏好如下:
对于标头:
对于源:
通常要避免的指针或建议冲突和循环引用,否则这都是个人偏好或您喜欢遵守协作项目的任何政策。
I'm not aware of any verbatim standard but as a general rule of thumb include as little headers as possible especially within other header files to reduce compile times, conflicts, and dependencies. I'm a fan of using forward declaration of classes in header files and only including the header and definition on the .cpp side whenever I can afford to do so.
That said my personal preference is below:
For Headers:
For Source:
Pointers or suggestions are usually to avoid conflicts and circular references, otherwise it's all personal preference or whatever policy you prefer adhere to for collaborative projects.
关于谷歌的风格:
一点也不含糊。
包含的第一个标头应该是与
this
源文件相关的标头,因此位于位置 1。通过这种方式,您可以确保它包含所需的任何内容,并且不存在“隐藏”依赖项:如果存在,它会立即暴露并阻止编译。其他标头按照您最不可能更改的标头排序(如果您最有可能更改的标头出现问题)。问题可能是标识符冲突、宏泄漏等……
根据定义,C 和 C++ 系统头文件很少被更改,仅仅是因为有很多人使用它们,因此它们排在第二位。
第三方代码可以更改,但通常很麻烦并且需要时间,因此它们排在第三位。
“项目包含”指的是项目范围的包含,通常是由多个项目使用的自制库(中间件)。它们可以改变,但这也会影响其他项目,它们排在第四位。
最后是“本地包含”,即特定于该项目的那些文件,可以在不影响其他任何人的情况下进行更改。如果出现问题,这些是主要候选人,他们是最后的。
请注意,实际上您可以有更多层(尤其是在软件商店中),关键思想是从底层(系统库)到顶层对依赖项进行排序。
在给定的层中,我倾向于按字母顺序组织它们,因为这样更容易检查它们。
Regarding Google's style:
There is no ambiguity, at all.
The first header included should be the header related to
this
source file, thus in position 1. This way you make sure that it includes anything it needs and that there is no "hidden" dependency: if there is, it'll be exposed right away and prevent compilation.The other headers are ordered from those you are the least likely to be able to change if an issue occurs to those you are the more likely to. An issue could be either an identifier clash, a macro leaking, etc...
By definition the C and C++ systems headers are very rarely altered, simply because there's so many people using them, thus they come second.
3rd party code can be changed, but it's generally cumbersome and takes time, thus they come third.
The "project includes" refer to project-wide includes, generally home-brawn libraries (middle-ware) that are used by several projects. They can be changed, but this would impact the other projects as well, they come fourth.
And finally the "local includes", that is those files who are specific to this project and can be changed without affecting anyone else. In case of issue, those are prime candidates, they come last.
Note that you can in fact have many more layers (especially in a software shop), the key idea is to order the dependencies starting from the bottom layer (system libs) to the top layer.
Within a given layer, I tend to organize them by alphabetical order, because it's easier to check them.
对于标题:
这个项目的标题
其他项目标题
第 3 方标头
源代码的C++ 标头
:
这个源文件的头
这个项目的标题
其他项目标题
第 3 方标头
C++ 标头
此命令可最大程度地减少在 .hpp 文件中遗漏某些所需标头的机会。它还最大限度地减少了第 3 方标头等的交集。并且所有 hpp 模块都以最小所需的依赖项进行编译。
例如:
->test.hpp
->test.cpp
->test2.cpp
For Headers:
this project's headers
other project headers
3rd party headers
C++ headers
For Source:
this source file's header
this project's headers
other project headers
3rd party headers
C++ headers
This orders minimize the chance to MISS some required header inside .hpp file. Also it minimize INTERSECTIONS of 3rd party headers etc. And all hpp modules compiles with minimum required dependensis.
for example:
->test.hpp
->test.cpp
->test2.cpp