Код: Выделить всё
std::optional foo()
{
auto cond = compute_condition();
if (cond) return compute_value();
return std::nullopt;
}
< /code>
И это работает нормально. Но - я хочу одну строку. Мне бы понравилось , чтобы писать:
std::optional foo()
{
return (compute_condition()) ? compute_value() : std::nullopt;
}
< /code>
Но это не может компилироваться (Godbolt.org): < /p>
error: operands to '?:' have different types 'T' and 'const std::nullopt_t'
Примечание: этот вопрос связан, но менее общий.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... i-write-in