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';
}
}
答案 0 :(得分:11)
您需要遵循“库实施”链接
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.201z
在Library Fundamentals V1 TS Components(表1.5)下进行了描述。
那是因为std::optional
是一个库功能,而不是语言功能,正如其中一条评论所述。