Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17 std::optional in G++?

Tags:

c++

g++

c++17

cppreference.com - std::optional identifies std::optional as being available "since C++17". C++ Standards Support in GCC - C++1z Language Features Lists c++17 features. I do not see std::optional in the list. Were is std::optional 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';
    }
}
like image 811
CW Holeman II Avatar asked Jun 25 '17 14:06

CW Holeman II


Video Answer


1 Answers

You need to follow the "library implementation" link

https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.201z

it is described under Library Fundamentals V1 TS Components (Table 1.5).

That's because std::optional is a library feature, not a language feature, as mentioned in one of the comments.

like image 138
vsoftco Avatar answered Sep 30 '22 17:09

vsoftco