cppreference.com - std :: optional将std :: 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';
}
}
Run Code Online (Sandbox Code Playgroud)
vso*_*tco 12
您需要遵循"库实现"链接
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.201z
它在Library Fundamentals V1 TS Components(表1.5)下描述.
那是因为它std::optional是一个库功能,而不是语言功能,正如其中一条评论中所提到的那样.
| 归档时间: |
|
| 查看次数: |
1649 次 |
| 最近记录: |