#pragma一旦安全包括警卫?
我已经读过,使用 #pragma once
时会有一些编译器优化,这可能导致更快的编译。我认识到这是非标准的,因此可能构成跨平台兼容性问题。
I've read that there is some compiler optimization when using #pragma once
which can result in faster compilation. I recognize that is non-standard, and thus could pose a cross-platform compatibility issue.
这是东西在大多数现代编译器支持非windows平台gcc)
Is this something that is supported by most modern compilers on non-windows platforms (gcc)?
我想避免平台编译问题,但也希望避免后备保护的额外工作:
I want to avoid platform compilation issues, but also want to avoid the extra work of fallback guards:
#pragma once
#ifndef HEADER_H
#define HEADER_H
...
#endif // HEADER_H
我应该关注吗?
它应该适用于任何现代编译器,但我没有看到任何原因不使用标准 #ifndef
包含guard。它工作正常。一个警告是,GCC不支持它在版本3.4之前。
It should work on any modern compiler, but I don't see any reason not to use a standard #ifndef
include guard. It works just fine. The one caveat is that GCC didn't support it before version 3.4.
我还发现,至少在GCC,它识别标准 #ifndef
include guard并优化它,因此它不应该比 #pragma once
慢得多。
I also found that, at least on GCC, it recognizes the standard #ifndef
include guard and optimizes it, so it shouldn't be much slower than #pragma once
.