Implementation status of std::optional in g++/libstdc++?
Asked Answered
C

2

20

As I am working on a C++ library that will be released publicly around 2014, I currently have design choices to make. One of the very useful tool that will be released with C++14 is std::optional. I would like to know with what version of g++/libstdc++ I can expect to have it using -std=c++1y.

Cide answered 9/6, 2013 at 20:9 Comment(2)
I changed the tag to C++1y per the Meta discussion linked to on the C++14 tag wiki that resulted in a decision not to use c++14 until the Final Draft Standard is released.Hygienics
if anyone wants to use the reference implementation, it's here.Twoway
L
20

Update (Oct 2016): std::optional was not in the final C++14 standard, so it is never available with -std=c++1y. Instead GCC 4.9 includes std::experimental::optional which is avilable with -std=c++1y.

GCC 7 will include std::optional with -std=c++1z i.e. C++17.

Original answer (Jun 2013):

It's currently unimplemented, see http://gcc.gnu.org/ml/libstdc++/2013-06/msg00032.html for the status of C++14 features in GCC's trunk.

When it gets implemented depends on when someone volunteers to do the work, so is impossible to predict.

I would expect it will be done for GCC 4.9, which should be released shortly before the C++14 standard, but there are no guarantees. It is unlikely to get added to GCC 4.8 though, as that's already released and unlike GCC 4.9 (i.e. the GCC subversion trunk) the __cplusplus macro has the same value for -std=c++11 and -std=c++1y so for GCC 4.8 there's no way to selectively enable features for -std=c++1y only.

Lail answered 9/6, 2013 at 20:44 Comment(4)
"why ask this on SO" - so people who search for answers on SO find it!Piccadilly
This was the top google hit when I was investigating why std::optional didn't work in gcc ! So I'm glad it was asked and answered here.Piccadilly
If the question had been asked on the gcc-help list that would have been the top hit. Anyway, this answer was wrong until yesterday, because there was no such thing as std::optional until C++17, so it didn't work in any compiler until very recently.Lail
It simply gets people SO rep. Asking on mailing list won't :pHotbed
U
6

As per their libstdc++ status page GCC 7.1 is the first version to have std::optional without being hidden in std::experimental and it supports the __has_include(<optional>) feature test. The feature test __cpp_lib_optional >= 201603 was added in GCC 7.3.

Usurpation answered 3/4, 2018 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.