12 Dec
2013
12 Dec
'13
3:02 p.m.
2013/12/12 Peter Dimov
Krzysztof Czainski wrote:
Whether this or try_lexical_cast, I prefer the function to return an optional.
As I said, it's appealing, but I suspect that it will prove much less convenient in practice.
struct X { int a; int b; };
bool parse( X& x, std::string a, std::string b ) { return try_lexical_cast( a, x.a ) && try_lexical_cast( b, x.b );
}
True, but I don't think you should leave it that simple. When parsing of a succeeds, and parsing of b fails, you get x with new value of a, and old value of b. This may or may not be acceptable, and at the very least deserves a heads-up comment. Regards, Kris