簡體   English   中英

C ++ 17 std :: G ++中的可選項?

[英]C++17 std::optional in G++?

cppreference.com - std :: optionalstd :: optional標識為“自C ++ 17以來”。 GCC中的C ++標准支持 - C ++ 1z語言特性列出了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';
    }
}

您需要遵循“庫實現”鏈接

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

它在Library Fundamentals V1 TS Components(表1.5)下描述。

那是因為std::optional是一個庫特性,而不是語言特性,正如其中一條評論中所提到的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM