Site icon Learn C++

How To Use std::u16string In A Modern C++ App

How To Use stdu16string In A Modern C++ App

What is u16string in modern C++? How can I use u16string in a C++ app? Is std::u16string same as std::string? Why I have an error when I define a std::u16string? Which literal should I use with the std::u16string?

What is u16string?

The u16string (std::u16string or std::pmr::u16string) are the string class data types for the 16bit characters defined in the std and std::pmr namespaces. It is a string class for 16-bit characters.

This is an instantiation of the basic_string class template that uses char16_t as the character type, with its default char_traits ad allocator types. In example, the std:string uses one byte (8 bits) while the std::u16string uses two bytes (16bits) per each character of the text string. In basic string definition std::u16string is defined as std::basic_string<char16_t>. Type definition can be shown as below,

[crayon-662d4f6e60055192894953/]

Note that, several string types for common character types are provided by basic string definitions as below,

String TypeBasic String DefinitionStandard
std::string std::basic_string<char>
std::wstringstd::basic_string<wchar_t>
std::u8stringstd::basic_string<char8_t>(C++20)
std::u16stringstd::basic_string<char16_t>(C++11)
std::u32stringstd::basic_string<char32_t> (C++11)

[crayon-662d4f6e6005c979258418/]

A simple example of std::u16string in a modern C++ app

Here is a simple example to use u16string,

[crayon-662d4f6e6005e012923879/]

as you see different string data types requires different ‘L’,’u’ and ‘U’ literals.

L, u and U are String Literals here, represents the type of characters of string. These might be default in you editor and or compiler options that means you don’t need to add if you know the default. A string literal is a sequence of characters surrounded by double quotes, optionally prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, as in “…”, R”(…)”, u8″…”, u8R”(…)“, u”…”, uR”˜(…)˜”, U”…”, UR”zzz(…)zzz”, L”…”, or LR”(…)”, respectively. Please see String Literals section in this document Working Draft, Standard for Programming Language C++. Here below we sum some of these standards used in C++.

Examples to String Literals for Strings Definitions

What is difference between L”” and U”” and u”” literals in C++

Full modern C++ example of using std::u16string

[crayon-662d4f6e60060893392284/]

In the latest versions of C++ Builder (10 and above), Strings are Unicode Strings. Unicode strings are easy to use in world-wise languages with many methods. Unicode standard for UnicodeString provides a unique number for every character (8, 16 or 32 bits) more than ASCII (8 bits) characters. UnicodeStrings are being used widely because of support to languages worldwide and emojis. In modern C++ nowadays there are two types of strings used; the array of chars (char strings) and UnicodeStrings (WideStrings and AnsiStrings are older, not compatible with all features now). CLANG / C++ Builder / GNU C / VC++ compilers, IDEs are using this standard for GUI forms to support all languages to provide applications in global. More information about the structure of Unicode Strings can be found here . RAD Studio, Delphi & C++ Builder uses Unicode-based strings: that is, the type String is a Unicode string (System.UnicodeString) instead of an ANSI string. If you want to transform your codes to Unicode strings we recommend you this article.

Here is a post about how to use Unicode Strings,

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version