Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

std::optional is in C++ 17, where it was std::experimental::optional before.

I tried compiling a file which included <optional>, with the command:

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

(in the Bash terminal). I get the following error:

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

But I can #include <experimental/optional> just fine.

Am I missing some header files? How can I include the optional header?

I also can't include <any>, <string_view> or <variant>, getting the same error.

like image 845
Artyer Avatar asked May 10 '17 10:05

Artyer


2 Answers

You can't.

GCC 6.2's support for C++17 is experimental, which is literally why the headers are arranged like this.

If only they'd done this for std::regex back in the day! It's a gift.

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

like image 61
Lightness Races in Orbit Avatar answered Oct 11 '22 05:10

Lightness Races in Orbit


I made a hacky workaround when faced with a situation like this:

#if defined(__GNUC__) && __GNUC__ < 7
# include <experimental/string_view>
# define string_view experimental::string_view
#else
# include <string_view>
#endif
like image 30
S.S. Anne Avatar answered Oct 11 '22 06:10

S.S. Anne