C++C++11C++14C++17Learn C++

This Is How To Use std::u32string In C++ Software

What is u32string in modern C++? How can I use u32string in C++ software? Is std::u32string same as std::string? Why do I have an error when I define a std::u32string? Which literal should I use with the std::u32string? Let’s answer these questions in this post.

What is u32string?

The u32string (std::u32string or std::pmr::u32string) are the string class data types for the 32bit characters defined in the std and std::pmr namespaces. It is a string class for 32bit 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::u32string uses four bytes (32bits) per each character of the text string. In basic string definition std::u32string is defined as std::basic_string<char32_t>. Type definition can be shown as below,

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)

A simple example of using std::u32string in modern C++ software

Here is a simple example to use u32string,

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

  • str=”abcd”; default string based on compiler/IDE options.
  • str=u8″abcd”; a UTF-8 string literal and is initialized with the given characters as encoded in UTF-8, including the null terminator
  • str=u”abcd”; a char16_t string literal. A char16_t string literal has type “array of n const char16_t”, including the null terminator
  • str=U”abcd”; a char32_t string literal. A char32_t string literal has type “array of n const char32_t”, including the null terminator
  • str=L”abcd”; a wide string literal. A wide string literal has type “array of n const wchar_t”, including the null terminator
  • str=R”abcd”; raw strings

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

  • L is based on wide string literal depends on array of n const wchar_t in your compiler/IDE options. Generally it is UTF-8 or UTF-16 format
  • u is for UTF-16 format,
  • U is for UTF32 formats

Here is a full C++ software example of using std::u16string

In C++ Builder, use the awesome UnicodeString

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.

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?