UTF-16 streams

Tom Cook tom.k.cook@gmail.com
Wed Feb 4 07:07:00 GMT 2015


I was a bit surprised to find that this doesn't work:

#include <sstream>
#include <string>

int main() {
    std::basic_stringstream<char16_t> ss;
    std::basic_string<char16_t> s(u"Hello, world.");
    ss << s;
    ss >> s;

Specifically, the last line of main causes a std::bad_cast to be
thrown, with the following stack trace:

(gdb) bt
#0  0x00007ffff7b2cea0 in __cxa_throw () from
#1  0x00007ffff7b832e2 in std::__throw_bad_cast() ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x0000000000403840 in std::__check_facet<std::ctype<char16_t> > (__f=0x0)
    at /usr/include/c++/4.8/bits/basic_ios.h:49
#3  0x0000000000402899 in std::basic_istream<char16_t,
std::char_traits<char16_t> >::sentry::sentry (this=0x7fffffffdbc7,
__in=..., __noskip=false)
    at /usr/include/c++/4.8/bits/istream.tcc:60
#4  0x0000000000401b31 in std::operator>><char16_t,
std::char_traits<char16_t>, std::allocator<char16_t> > (__in=...,
__str=u"Hello, world")
    at /usr/include/c++/4.8/bits/basic_string.tcc:1009
#5  0x000000000040139d in main () at test2.cc:8

(This is for GCC 4.8; 4.9 gives similar results).

AFAICT this is due to missing character traits for char16_t - have I
got that right?

Is this expected behaviour?  Is it relatively straightforward to patch
this so that it works?  Could someone give me a pointer in the right


More information about the Libstdc++ mailing list