Google C ++样式指南包含顺序 [英] Google C++ Style Guide include order

查看:74
本文介绍了Google C ++样式指南包含顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google C ++样式指南建议包括头文件(.h)到实现文件(.cpp,.cc)的顺序如下:

Google C++ Style Guide recommends to include the header files (.h) to the implementation files (.cpp, .cc) in the following order:

dir/foo.cc dir/foo_test.cc 中,其主要目的是实现或测试 dir2/foo2.h <中的内容/code>,请按以下顺序订购包含的商品:

In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:

dir2/foo2.h.
A blank line
C system files.
C++ system files.
A blank line
Other libraries' .h files.
Your project's .h files.

如前所述,这样的顺序允许在编译 foo -unit而不是其他无害的单元时,在 dir2/foo2.h 中看到省略的依赖项.看起来合乎逻辑.

As said such order allows to see the omitted dependencies in dir2/foo2.h while compiling the foo-unit, rather than other innocent units. Seems quite logically.

但是为什么将其他库的.h文件.项目的.h文件.放在列表的末尾?从理论上讲,也可能缺少依赖项,这些依赖项将通过包含以前的 C系统文件 C ++系统文件而被隐藏.

But why Other libraries' .h files. and Your project's .h files. placed to the end of the list? In theory there also could be missing dependencies which will be hidden by including the C system files. and C++ system files. before.

也许假设应该在相关的实现文件中检测到其他(头文件)问题?在这种情况下,仅标头库呢?

Maybe there is assumed that other (header) files problems should be detected in related implementation files? What about header-only libraries in that case?

换句话说,将包括以下顺序:

In other words, would the following order of includes:

dir2/foo2.h.
A blank line
Other libraries' .h files.
Your project's .h files.
A blank line
C system files.
C++ system files.

更好地更快地找到隐藏的依赖项吗?

will be better to more fast finding hidden dependencies?

例如,如果我只有需要< stdio.h> 的标头(但未在该文件中指定).使用Google顺序,直接或间接包含< stdio.h> 的概率要比在最后一步中包含系统文件时高(如我之前所建议).因此,发现隐藏依赖性的可能性很小.那么,如果在其他lib/您的项目文件之前包含系统文件,我们将赢得什么呢?

For instance, if I have only header that requires <stdio.h> (but not specified in that file). Using the Google order the probabily of direct or indirect include of <stdio.h> is higher, than when the system files are included on the last step (as I suggested before). Hence, low probability to find hidden dependency. So what we will win if include the system files before other lib/your project files?

还不清楚,我应该在其他(用户定义的)头文件中使用推荐的包含文件顺序吗?

Also it's not clear, should I use recommended order of include files in other (user defined) header files.

推荐答案

每个头文件(h,hpp,...)应该有一个实现文件(cpp,cc,...),其中包含的顺序是与问题中指定的相同.(即使授权文件为空,除了这1个include之外)

Every header file(h, hpp,...) should have an implementation file (cpp, cc,...) where the order of including is the same as specified in the question. (Even if the implentation file is empty, except for this 1 include)

因此,就像您的"标头首先包含在您的"实现文件中一样,因此每个其他"标头文件也应首先包含在其他"实现文件中.

So just like "Your" header is included first in "Your" implementation file, so every "other" header file should be included first in the "other" implementation file.

这样,如果其他"标头不包含必需的标头,则其他"实现文件将不会编译.

This way if the "other" header does not include a required header, the "other" implementation file will not compile.

这篇关于Google C ++样式指南包含顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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