C ++ 17 std :: optional in g ++?
cppreference.com - std :: optional identifies std :: optional as available "since C ++ 17". C ++ Standards Support in GCC Language Features - C ++ 1z C ++ 17 Lists I don't see std :: optional in the list. Has std :: optional been documented for g ++?
#include <string>
#include <iostream>
#include <optional>
// optional can be used as the return type of a factory that may fail
std::optional<std::string> create(bool b) {
if(b)
return "Godzilla";
else
return {};
}
int main()
{
std::cout << "create(false) returned "
<< create(false).value_or("empty") << '\n';
// optional-returning factory functions are usable as conditions of while and if
if(auto str = create(true)) {
std::cout << "create(true) returned " << *str << '\n';
}
}
+3
source to share
1 answer
You need to follow the link "library implementation"
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.201z
described in the "Fundamentals of the V1 TS library" (table 1.5).
This is because std::optional
- it is a library function, not a language function as stated in one of the comments.
+11
source to share