1 #include <iostream> 2 #include <optional> 3 #include <string> 4 5 std::optional<std::string> create(bool b) 6 { 7 if(b) return "Godzilla"; 8 return {}; 9 } 10 11 int main() 12 { 13 std::cout << "create(false) return: " << create(false).value_or("empty") << std::endl; 14 std::cout << "create(true) return: " << create(true).value_or("empty") << std::endl; 15 return 0; 16 }
输出:
create(false) return: empty create(true) return: Godzilla