How do I put strings of type 'char32_t' and 'char16_t' in the console in C ++?


In the new C ++ 17 they added the characters char32_t and char16_t , they also added 'u16string' and 'u32string', but they did not create any way to put them on the console screen.

For example:

int main()
    std::u16string u16s{ u"meu teste" }; //até aqui tudo bem.
    std::cout << u16s << std::endl; //não compila, std::cout não aceita u16string
    //não existe std::u16cout nem std::u32cout como existe std::wcout 
asked by anonymous 13.08.2018 / 03:19

1 answer


The way to go is to use std::codecvt_utf8_utf16 and std::wstring_convert (although discontinued in

#include <iostream>
#include <locale>
#include <codecvt>
#include <string>

int main()
    std::u16string u16str = u"meu teste";
    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> codecvt;
    std::string u8str = codecvt.to_bytes(u16str);
    std::cout << u8str << '\n';

With the discontinuation of these classes, it is not possible to convert using only the default library. However, there is a new Unicode study group on the C ++ committee. We will probably see new APIs for this kind of language work.

13.08.2018 / 19:23