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
是一个库功能,而不是一种语言功能,如其中一条评论中所述。
关于C++ 17 std::optional 在 G++ 中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44747264/