C ++ 17 std :: G ++中的可选项?

时间:2017-06-25 14:20:12

标签: c++ g++ c++17

cppreference.com - std::optional将std :: optional标识为可用"因为C ++ 17"。 C++ Standards Support in GCC - C++1z Language Features列出了c ++ 17的功能。我没有在列表中看到std :: optional。是否为G ++记录了std :: optional?

#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';
    }
}

1 个答案:

答案 0 :(得分:11)

您需要遵循“库实施”链接

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

在Library Fundamentals V1 TS Components(表1.5)下进行了描述。

那是因为std::optional是一个库功能,而不是语言功能,正如其中一条评论所述。