
Vladimir Batov wrote:
... I like the lexical_cast< int >("x", -1); syntax much more.
If the syntax is considered unacceptable, I can suggest another alternative:
int x = lex_cast< int >("x").with_default(10); int y = lex_cast< int >("y").or_default_constructed();
I sympathize with the idealistic desire for lexical_cast to look like the dynamic_cast et al. The problem is that no matter how you make lexical_cast look it does not do what other language-supported casts do -- dynamic_cast el al work with *types*, lexical_cast works with *values*. Making lexical_cast look like what it is not (the "real" casts) has no practical value and is just misleading.
I disagree. Making lexical_cast behave similar to other casts makes perfect sense. It does change the type of its argument just as well as its physical representation.
Now seemingly out of desperation people are venturing into
lexical_cast<optional> lex_cast< int >("x").with_default(10); lex_cast< int >("y").or_default_constructed();
It's just crazy from the usability point of view. Are you sure users will appreciate all that mounting complexity on top of lexical_cast? All that is needed
lexical_cast(Source const&, Target const& =Target())
No, that kind of implementation seems unacceptable to me. I want lexical_cast to throw exceptions just as often as I don't want it to. And, of course, I'm against introducing a breaking change.
or most likely a completely independent overload
lexical_cast(Source const&, Target const&)
That would be my preferred way to implement this extension.