Characters
In the C++ standard library, a character is an object which, when treated sequentially, can represent text.
The term means not only objects of
, but also any value that can be represented by a type that provides the definitions specified in the strings library and following libraries:
In the strings library and regular expressions library(since C++11), a character can be of only char-like types, i.e. those non-array types that satisfy the requirements of
(until C++20)
and
(since C++20)(until C++26)
and
(since C++26).
For any char-like type T, std::is_trivially_default_constructible_v<T> is true.
(since C++26)Therefore, characters are also referred as char-like objects in the strings library and regular expressions library(since C++11).
Some standard library components accept character container types. They, too, are types used to represent individual characters. Such types are used for one of the template arguments of
and the class templates which use
.
Library components
The C++ strings library includes the following components:
Character traits
Many character-related class templates (such as
) need a set of related types and functions to complete the definition of their semantics. These types and functions are provided as a set of member typedef names and functions in the template parameter Traits used by each such template. The classes which are able to complete those semantics are
.
The string library provides the class template
that defines types and functions for
and
(since C++17).
The following specializations are defined, all of them satisfy the
requirements:
Defined in header
template<>classchar_traits<char>;template<>classchar_traits<wchar_t>;template<>classchar_traits<char8_t>;(since C++20)template<>classchar_traits<char16_t>;(since C++11)template<>classchar_traits<char32_t>;(since C++11)When a user-defined character container type for
and
(since C++17) is used, it is also necessary to provide a corresponding character trait class (which can be a specialization of
).
String classes (
etc.)
The class template
generalizes how sequences of characters are manipulated and stored. String creation, manipulation, and destruction are all handled by a convenient set of class methods and related functions.
Several specializations of
are provided for commonly-used types:
Defined in header
Type Definition std::stringstd::basic_string<char>std::wstringstd::basic_string<wchar_t>std::u8string(since C++20)std::basic_string<char8_t>std::u16string(since C++11)std::basic_string<char16_t>std::u32string(since C++11)std::basic_string<char32_t>String view classes (
etc.) (since C++17)
The class template
provides a lightweight object that offers read-only access to a string or a part of a string using an interface similar to the interface of
.
Several specializations of
are provided for commonly-used types:
Defined in header
Type Definition std::string_viewstd::basic_string_view<char>std::wstring_viewstd::basic_string_view<wchar_t>std::u8string_view(since C++20)std::basic_string_view<char8_t>std::u16string_viewstd::basic_string_view<char16_t>std::u32string_viewstd::basic_string_view<char32_t>Relevant libraries
The
provides support for localizations, string conversions (e.g.
), character classification functions (e.g.
), and text encoding recognition (
).
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 char-like types could be array types prohibited See also