This gives a bad_lexical_cast exception:
int8_t i = boost::lexical_cast<int8_t>("12");
I would like to have an exception when the value doesn't fit in an int8_t.
How should I do this? Should I cast to an int first and throw an exception if the value falls outside the range -128 to 127?
I'm also interested in converting strings to uint8_t.
Q: What does lexical_cast of an int8_t or uint8_t not do what I expect?
A: As above, note that int8_t and uint8_t are actually chars and are formatted as such. To avoid this, cast to an integer type first
Source:
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_lexical_cast/frequently_asked_questions.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With