在头文件中何时不使用包含保护?

时间:2022-10-11 02:04:53

We all know when to use include guard, but when shall we not use it in our project?

我们都知道什么时候使用include guard,但什么时候不在我们的项目中使用它呢?

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.

最近,我看到了一个混合编译(CUDA + GCC)的项目,一个头文件(CUDA文件)被故意留下而没有包含保护。我只是好奇而已。

4 个解决方案

#1


12  

There are 2 scenarios off the top of my head:

我脑海中有两种场景:

  1. when you want to turn on/off debugging capabilities (as how assert.h works)
  2. 当您想要打开/关闭调试功能时(如如何断言)。h工作)
  3. for 'x-macro' type of functionality where you have the include file perform 2 parts of problem, such as defining an enum then defining an array of stringified names corresponding to the enums
  4. 对于包含文件的“x-macro”类型的功能,可以执行问题的两个部分,例如定义枚举,然后定义与枚举对应的字符串化名称数组

#2


2  

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文件将作为一种模板。Dosbox上的标量就是一个例子。

#3


2  

In our projects we never use include guard. We are using include antiguard:

在我们的项目中,我们从不使用include guard。我们正在使用防护罩:

#ifndef _stdafx_h_
#define _stdafx_h_
#else
#error reinclude stdafx.h
#endif

Because if you reincluded same header - you written wrong code or worked with wrong architecture.

因为如果您重新包含了相同的标题——您编写了错误的代码或使用了错误的架构。

#4


0  

Include guards are used so that the include file can be included multiple times in a single compilation unit without resulting in duplicate declarations.

使用Include保护,以便在单个编译单元中多次包含Include文件,而不会导致重复声明。

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.

当文件应该在一个编译单元中包含多次并且不会导致重复声明时,不要使用include保护。

#1


12  

There are 2 scenarios off the top of my head:

我脑海中有两种场景:

  1. when you want to turn on/off debugging capabilities (as how assert.h works)
  2. 当您想要打开/关闭调试功能时(如如何断言)。h工作)
  3. for 'x-macro' type of functionality where you have the include file perform 2 parts of problem, such as defining an enum then defining an array of stringified names corresponding to the enums
  4. 对于包含文件的“x-macro”类型的功能,可以执行问题的两个部分,例如定义枚举,然后定义与枚举对应的字符串化名称数组

#2


2  

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文件将作为一种模板。Dosbox上的标量就是一个例子。

#3


2  

In our projects we never use include guard. We are using include antiguard:

在我们的项目中,我们从不使用include guard。我们正在使用防护罩:

#ifndef _stdafx_h_
#define _stdafx_h_
#else
#error reinclude stdafx.h
#endif

Because if you reincluded same header - you written wrong code or worked with wrong architecture.

因为如果您重新包含了相同的标题——您编写了错误的代码或使用了错误的架构。

#4


0  

Include guards are used so that the include file can be included multiple times in a single compilation unit without resulting in duplicate declarations.

使用Include保护,以便在单个编译单元中多次包含Include文件,而不会导致重复声明。

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.

当文件应该在一个编译单元中包含多次并且不会导致重复声明时,不要使用include保护。