When not to use include guard in header file?

馋奶兔 提交于 2019-12-10 01:23:59

问题


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.


回答1:


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. 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



回答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.




回答3:


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

#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:


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.



来源:https://stackoverflow.com/questions/6793161/when-not-to-use-include-guard-in-header-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!