boost::lexical_cast throwing exception during converting string to int8_t, but int32_t - norm.
What can be wrong with int8_t ?
#include <iostream>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
int main()
{
try
{
const auto a = boost::lexical_cast<int8_t>("22");
std::cout << a << std::endl;
}
catch( std::exception &e )
{
std::cout << "e=" << e.what() << std::endl;
}
}
For
boost::lexical_cast, the character type of the underlying stream is assumed to be char unless either the Source or the Target requires wide-character streaming, in which case the underlying stream uses wchar_t. Following types also can use char16_t or char32_t for wide-character streamingBoost Lexical Cast
So after doing below changes in your code:
Gives below output
So, I feel that each character is taken as
char.So, for
const auto a = boost::lexical_cast<int16_t>("2");2 is taken as singlecharwhich require int8_t.And, for
const auto b = boost::lexical_cast<int16_t>("22");22 is taken as twocharvalues which require int16_t.I hope it helps!