Boost logo

Boost :

From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2007-05-07 12:01:40

Dean wrote:
> I've recently tried the following (inlined test) which isolates the
> problem I've encountered with boost::lexical_cast<int8_t>:
> #include <iostream>
> #include <boost/lexical_cast.hpp>
> int main(int argc, char * argv[]) {
> int8_t value;
> try {
> value = boost::lexical_cast<int8_t>("127");
> } catch (std::exception & e) {
> std::cerr << e.what() << '\n';
> }
> return 0;
> };
> This is compiled with gcc 4.1.2 on Linux. Any idea why 127 wouldn't
> fit into an 8-bit signed integer? The defined range for signed 8 bit
> integers should be -127..+127 right?

Hi Dean,

You haven't said what you expected to happen and what happened
instead. So here's a guess: you expect int8_t to be treated as an
integer by lexical_cast. It isn't; despite the name, it's a character;
you are asking it to convert a string to a character. (Try some other
strings where you have "127". I think it will reject anything with
more than one character in it.)

I have previously suggested that this behaviour should change, but the
opinion of the list has been that using int8_t probably indicates
"over-optimised code", and that the feature can be easily worked around
with some additional casts.

In this case, I suggest that you lexical_cast to int, do a bounds check
yourself (if necessary) and then assign to the int8_t.



Boost list run by bdawes at, gregod at, cpdaniel at, john at