Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::u16cout missing?

C++03 defines two character types: char and wchar_t. (lets ignore the signed char and unsigned char insanity).

These two character are then applied to std::basic_string, std::basic_ostream, etc as std::string/std::wstring and std::ostream/std::wostream.

From the streams the standard library also defines the globals std::cout and std::wcout.

The new c++0x standard defines two more character types char16_t and char32_t. However, the only new typedefs are std::u16string and std::u32string.

Why doesn't the standard supply a std::u16ostream? Or how about a std::u32cout?

like image 838
deft_code Avatar asked May 16 '11 16:05

deft_code


2 Answers

It was decided that implementing Unicode iostreams was too much work to be worth it: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2238.html

From the paper:

The rationale for leaving out stream specializations of the two new types was that streams of non-char types have not attracted wide usage, so it is not clear that there is a real need for doubling the number of specalizations of this very complicated machinery.

From what I understand, the standard committee realized that serialization to wide character (2- or 4-byte formats) is uncommon, and where you'd need UTF-16 or UTF-32 you could always implement it yourself using the same old char-based byte streams, but with a codecvt facet to would convert your input to UTF-16/UTF-32, which it could treat as yet-another-multibyte-format.

like image 51
Boaz Yaniv Avatar answered Sep 20 '22 14:09

Boaz Yaniv


I don't know the official reason.

But I don;t see the need for one.
By having streams that are of a specific type you are hard coding there usage. I would prefer streams that are generic (handle bytes) that you can then customize to output to a specific format. Like they currently work.

So internally I want to use UTF16 strings. But on output I want to serialize them to UTF8 for storage. For this I would simply expet to create a normal stream imbue it with a locale that knows how to convert to from UTF16 -> UTF8 then all the stream needs to do is handle bytes.

Having the stream understand the format on disk byes you very little. Having a locale that can convert between different formats (on the device to internal and vic versa) is very convenient.

like image 27
Martin York Avatar answered Sep 18 '22 14:09

Martin York