
7 Feb
2009
7 Feb
'09
10:25 a.m.
Scott McMurray wrote:
One very clear use:
if (optional<int> oi = lexical_cast<optional<int> >(mystring)) { int i = *oi; ... use i ... } else { ... parse failed ... }
And if you want defaults:
int i = -1; if (optional<int> oi = lexical_cast<optional<int> >(mystring)) { i = *oi; } ... use i ...
Or, using the helper:
int i = lexical_cast<optional<int> >(mystring).get_value_or(-1);
Why is this so bad?
Like I said: 1. It is longer and more clumsy than lexical_cast< int >(str, 1). It does introduce enough scaffolding that user has to type to avoid lexical_cast altogether in such cases. 2. It introduces dependency on optional. 3. It bans lexical_casting optionals.