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)下描述.