c++ - __has_include() 和随后的#include 之间是否存在竞争条件?

标签 c++ c++17

考虑以下使用 __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 of 1 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/

相关文章:

c++ - 编译器报告 'deleted' operator = ,但它在那里

c++ - C++17 的 std::inserter 替代品

c++ - 在 `if constexpr` block 内声明的变量范围

c++ - 将 std::vector<std::byte> 分配给 std::vector<char> 而不复制内存

c# - 关于变量名成员前缀

c++ - QCommandLine选项 : How to parse the same parameter multiple times?

c++ - 根据类模板的类型参数自动化类模板的大小参数

c++ - 如何使用参数化构造函数动态分配对象数组?

c++ - 从 vector 列表中删除 vector 元素 C++

c++ - 对 Boost 线程的 undefined reference