c++ - std::bad_cast when using std::basic_istringstream<unsigned short> -
i'm trying process utf-16 string (placed in buffer buf
) of std::basic_string
, istringstream
. exception std::bad_cast
occurs in code. there problem code? or gcc's stl cannot handle unsigned int
(16 bit) symbols?
const unsigned short * buf; // ... fiilling buf std::basic_string<unsigned short> w(buf); std::basic_istringstream<unsigned short> iss(w); unsigned int result; try { iss >> result; } catch (std::exception& e) { const char * c = e.what(); }
the same code std::wstring
, std::wistringstream
works correctly.
instantiation of iostreams on different character types char
, wchar_t
rather non-trivial. streams need number of std::locale
facets present. without them won't function properly. attempted operation you'd need, @ least:
std::ctype<ct>
std::numpunct<ct>
std::num_get<ct>
where ct
stream's character type. last 1 of these should require instantiation others need implemented. of course, need make sure std::locale
installed stream either setting global locale or using stream.imbue()
.
personally, think overall wrong approach, though: characters should converted internal representation when entering system , converted external representation when leaving system (that's purpose of std::codecvt<...>
facet). seems, however, lost fight , people feel want mess encodings internally.
Comments
Post a Comment