我經常需要使用的功能可選類型:返回可選值有:運營商
std::optional<int32_t> get(const std::string& field)
{
auto it = map.find(field);
if (it != map.end()) return it->second;
return {};
}
有沒有辦法在一個行返回可選值?例如這樣的:
std::optional<int32_t> get(const std::string& field)
{
auto it = map.find(field);
return it != map.end() ? it->second : {};
}
導致錯誤
error: expected primary-expression before '{' token
return it != map.end() ? it->second : {};
^
@ tobi303'{}'未解析爲表達。 – MiP
Clang給出了一個更好的錯誤消息:「初始化程序列表不能在運算符的右側使用?」 – emlai