考虑以下使用 __has_include()
的 C++1z 代码:
#if __has_include(<optional>)
# include <optional>
# define have_optional 1
#else
# define have_optional 0
#endif
__has_include(<optional>)
之间是否存在竞争条件?和随后的 #include <optional>
或者该标准是否保证无种族行为?例如,在(不可能的)情况下,头文件在 __has_include()
之后立即被删除。检查,#include
会意外失败。
最佳答案
虽然我认为这在很大程度上是一个特定于实现的问题,this #include
reference说
A
__has_include
result of1
only means that a header or source file with the specified name exists. It does not mean that the header or source file, when included, would not cause an error or would contain anything useful.
所以你不应该指望后续的 #include
指令成功。
上面的链接引用实际上延续了上面的引用,提到了同时具有 C++14 和 C++17 模式的编译器可能有 __has_include
。作为其 C++14 模式的扩展,以仅使用 <optional>
为例可能导致 __has_include(<optional>)
在 C++14 模式下成功,但实际 #include
失败。
关于c++ - __has_include() 和随后的#include 之间是否存在竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520327/