Вопрос

I see that it is allowed to write a char to a std::wostream (for example, std::wcout<<"looooool";).

How are the char changed to wchar (if that's what happens)?

Это было полезно?

Решение

When you send a char or a c-string (char *) to a wide stream, the ìndividual octets (bytes) are converted to wchar with widen. There is no automatic conversion from a std::string.

You cannot send multibyte UTF-8 characters into a wide stream this way, because the bytes are converted one at a time. In the default locale, there is no conversion from a non-ascii character to a wide character, so the conversion will fail, putting the wide stream into error state.

Whether you take advantage of this conversion or not is up to you; the standard allows it, and for character and string literals, at least, it seems harmless to me. But do be aware that string objects you send to a wide stream must be std::wstring, not std::string.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top