如何将 C++ 17 headers 包含在带有 -std=c++17 的 g++ 6.2.0 中(可选,任意,string_view,变体)

How to include C++ 17 headers with g++ 6.2.0 with -std=c++17 (optional, any, string_view, variant)

std::optional 在 C++ 17 中,之前是 std::experimental::optional

我尝试使用以下命令编译一个包含 <optional> 的文件:

g++ -std=c++17 <filename>.cpp

(在 Bash 终端)。我收到以下错误:

<filename>.cpp:5:20 fatal error: optional: No such file or directory
 #include <optional>
                    ^
compilation terminated

但我可以#include <experimental/optional>就好了。

我是否遗漏了一些 header 文件?如何包含 optional header?

我也无法包含 <any><string_view><variant>,出现同样的错误。

你不能。

GCC 6.2's support for C++17 is experimental,这就是为什么 headers 这样排列的原因。

如果他们当年能为 std::regex 做到这一点就好了!这是一份礼物。

https://gcc.gnu.org/projects/cxx-status.html#cxx1z

遇到这样的情况时,我做了一个 hacky 解决方法:

#if defined(__GNUC__) && __GNUC__ < 7
# include <experimental/string_view>
# define string_view experimental::string_view
#else
# include <string_view>
#endif