什么时候不应该在头文件中使用包含保护?
我们都知道什么时候使用includeguard,但是什么时候我们的项目中不应该使用它呢?
最近看到一个混合编译的项目(CUDA + GCC),故意留下一个头文件(CUDA文件),没有包含保护。我只是好奇而已。
We all know when to use include guard, but when shall we not use it in our project?
Recently, I saw a project with mix compilation (CUDA + GCC), one header file (CUDA file) is deliberately left without include guard. I am just curious about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想到了两种情况:
There are 2 scenarios off the top of my head:
assert.h
works)在我们的项目中,我们从不使用包含防护。我们正在使用 include antiguard:
因为如果您重新包含相同的标头 - 您编写了错误的代码或使用了错误的架构。
In our projects we never use include guard. We are using include antiguard:
Because if you reincluded same header - you written wrong code or worked with wrong architecture.
一种情况是您确实想使用不同的参数多次包含同一文件。在这种情况下,包含文件将充当一种模板。例如 缩放器。
One case in when you do want to include the same file several times with different parameters. In this case the include file would act as a sort of template. An example are the scalers on Dosbox.
使用包含防护,以便可以在单个编译单元中多次包含包含文件,而不会导致重复声明。
当文件应多次包含在单个编译单元中并且这不会导致重复声明时,请勿使用包含保护。
Include guards are used so that the include file can be included multiple times in a single compilation unit without resulting in duplicate declarations.
Do not use include guards when the file should be included multiple times in a single compilation unit and this does not result in duplicate declarations.