问题描述
我知道为什么包含保护存在,并且 #pragma once
不是标准的,因此不是所有编译器等支持。
I know why include guards exist, and that #pragma once
is not standard and thus not supported by all compilers etc.
我的问题是不同的:
有没有合理的理由,没有他们?我还没有遇到一种情况,理论上,没有提供包含卫士在一个文件,意图被包括在别处的任何好处。
Is there any sensible reason to ever not have them? I've yet to come across a situation where theoretically, there would be any benefit of not providing include guards in a file that is meant to be included somewhere else. Does anyone have an example where there is an actual benefit of not having them?
我要求的理由 - 对我来说,他们似乎很冗余,因为你总是使用它们,而且 #pragma once
的行为也可以自动应用于字面上的一切。
The reason I ask - to me they seem pretty redundant, as you always use them, and that the behaviour of #pragma once
could as well just be automatically applied to literally everything.
推荐答案
我看过生成代码的标头,这取决于在包含之前定义的宏。在这种情况下,它有时想要定义这些宏到一个(组)值,包括头,重新定义宏,并再次包括。
每个人谁看到这样的同意,它的丑陋和最好避免,但有时(如果在所述标题中的代码是通过一些其他方式生成的),这是更少的邪恶。
I've seen headers that generate code depending on macros defined before their inclusion. In this case it's sometimes wanted to define those macros to one (set of) value(s), include the header, redefine the macros, and include again.
Everybody who sees such agrees that it's ugly and best avoided, but sometimes (like if the code in said headers is generated by some other means) it's the lesser evil to do that.
除此之外,我不能想到一个理由。
Other than that, I can't think of a reason.
这篇关于有什么情况下你不想包括卫兵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!