u/FrostshockFTW

Is there a more elegant way to accumulate optional values?

Currently I'm doing this, which works fine but leaves me wanting.

std::optional<int> acc;
while(/*...*/) {
    if(auto maybe_val = Get_optional_value()) {
        *maybe_val += acc.value_or(0);
        acc = std::move(maybe_val);
    }
}
reddit.com
u/FrostshockFTW — 25 days ago