加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:14:10 所属栏目:百科 来源:网络整理
导读: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 iostrea
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';
    }
}

解决方法

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

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

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

那是因为std :: optional是一个库特性,而不是语言特性,正如其中一条评论中所提到的那样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读