2
cppreference.com - std::optional標識std :: optional爲自「C++ 17以來」可用。 C++ Standards Support in GCC - C++1z Language Features列出C++ 17的功能。我在列表中看不到std :: optional。是std ::可選爲G ++記錄?C++ 17 std :: G ++中可選?
#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';
}
}
'optional'不是*語言功能*。這是C++ 17的庫特性。所以它不會在語言功能部分列出。 –