How to use unicode in C++ without much pain?

In C++ there’s no solid standard when it comes to encoding. If I want to use unicode, for example, UTF-8 in C++ for Windows, how can I achieve that?

  1. On Windows I have to use something like wide-strings to use unicode, is it the only way?
  2. If I have to use third-party libraries, what libraries do you can advise?
  3. What I have to remember when using unicode instead of std::string?

>Solution :

If you are talking about source code, then its implementation specific for each compiler, but I believe every modern compiler supports UTF-8 at least.

C++ itself has following types to support Unicode:
wchar_t, char16_t, char32_t and char8_t for characters and corresponding std::wstring, std::u16string, std::u32string and std::u8string for strings.

And following notations for literals:

char8_t ch_utf8 = u8'c';
char16_t ch_utf16 = u'c';
char32_t ch_utf32 = U'C';
wchar_t ch_wide = L'c';

char8_t str_utf8[] = u8"str";
char16_t str_utf16[] = u"str";
char32_t str_utf32[] = U"str";
wchar_t str_wide[] = L"str";

std::codecvt template for string conversions between different encodings.

Leave a Reply