|
Boost : |
Subject: Re: [boost] [lexical_cast] cast of int8_t
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2012-10-16 16:28:43
> On 13/10/12 16:35, Jochen Wilhelmy wrote:
> another question is why does lexical_cast treat int8_t and uint8_t like
> char? technically I see no reason for this as c++ has three types: char,
> signed char and unsigned char. in my code I use only char for characters
> and signed/unsigned char as numbers. I know that the standard stream
> operator << treats signed/unsigned char as char but I know no
> explanation for this (I consider it a bug ;-)
Changing the current behavior will break existing user code. However
you can always use
lexical_cast<std::string>(static_cast<short>(int8_value))
or even make some wrapper:
template <class To>
To my_cast(unsigned char f) {
return boost::lexical_cast<To>(static_cast<int>(f));
}
template <class From, class To>
To my_cast(const From& f) {
return boost::lexical_cast<To>(f);
}
2012/10/14 John Bytheway <jbytheway+boost_at_[hidden]>:
> Whatever we think of the standard streams' decision (and indeed I agree
> it's dubious), lexical_cast behaves in this way because it is defined in
> terms of the standard streams. Personally I think that is the right
> choice. Consistency is more important than tweaking each individual case.
Totally agree.
-- Best regards, Antony Polukhin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk