CharTraits is a traits class that abstracts basic character and string operations for a given character type. Most standard library string and input/output classes require a CharTraits template type parameter alongside a corresponding character template type parameter.
No operation listed below on CharTraits may throw an exception.
Given.
CharT
, a character type X
, a CharTraits type for type CharT
c
, d
, values of type CharT
p
, q
, values of type const CharT* s
, a value of type CharT*
n
, i
, j
, values of type std::size_t
e
, f
, values of type X::int_type
pos
, a value of type X::pos_type
state
, a value of type X::state_type
r
, an lvalue of type CharT
expression | return type | semantics | complexity |
---|---|---|---|
X::char_type | CharT | Used to refer to the character type | Compile-time |
X::int_type | A type that can hold all valid values of X::char_type plus X::eof() | Compile-time | |
X::off_type | Invokes implementation-defined behavior if not std::streamoff when X is used as the traits template parameter in input/output classes. | Compile-time | |
X::pos_type |
| Compile-time | |
X::state_type | Destructible, CopyAssignable, CopyConstructible, DefaultConstructible | Compile-time | |
X::eq(c, d) | bool | Returns: whether c is to be treated as equal to d | Constant |
X::lt(c, d) | bool | Returns: whether c is to be treated as less than d | Constant |
X::compare(p, q, n) | int | Returns:
| Linear |
X::length(p) | std::size_t | Returns: the smallest i such that X::eq(p[i], CharT()) is true | Linear |
X::find(p, n, c) | const X::char_type* | Returns:
| Linear |
X::move(s, p, n) | X::char_type* |
| Linear |
X::copy(s, p, n) | X::char_type* |
| Linear |
X::assign(r, d) | (Not used) | Assigns r = d | Constant |
X::assign(s, n, c) | X::char_type* |
| Linear |
X::not_eof(e) | X::int_type | Returns:
| Constant |
X::to_char_type(e) | X::char_type | Returns:
| Constant |
X::to_int_type(c) | X::int_type | Returns: some value e , constrained by the definitions of X::to_char_type and X::eq_int_type | Constant |
X::eq_int_type(e, f) | bool |
| Constant |
X::eof() | X::int_type | Returns: a value e such that X::eq_int_type(e, X::to_int_type(c)) is false for all values c | Constant |
CharTraits is required by the following standard library class templates as a template type parameter:
Strings |
|
stores and manipulates sequences of characters (class template) |
|
(C++17) | read-only string view (class template) |
Streams |
|
manages an arbitrary stream buffer (class template) |
|
wraps a given abstract device (std::basic_streambuf )and provides high-level input interface (class template) |
|
implements high-level file stream input operations (class template) |
|
implements high-level string stream input operations (class template) |
|
(C++23) | implements fixed character buffer input operations (class template) |
wraps a given abstract device (std::basic_streambuf )and provides high-level output interface (class template) |
|
implements high-level file stream output operations (class template) |
|
implements high-level string stream output operations (class template) |
|
(C++20) | synchronized output stream wrapper (class template) |
(C++23) | implements fixed character buffer output operations (class template) |
wraps a given abstract device (std::basic_streambuf )and provides high-level input/output interface (class template) |
|
implements high-level file stream input/output operations (class template) |
|
implements high-level string stream input/output operations (class template) |
|
(C++23) | implements fixed character buffer input/output operations (class template) |
Stream iterators |
|
input iterator that reads from std::basic_istream (class template) |
|
output iterator that writes to std::basic_ostream (class template) |
|
Stream buffers |
|
abstracts a raw device (class template) |
|
implements raw file device (class template) |
|
implements raw string device (class template) |
|
(C++20) | synchronized output device wrapper (class template) |
(C++23) | implements raw fixed character buffer device (class template) |
Stream buffer iterators |
|
input iterator that reads from std::basic_streambuf (class template) |
|
output iterator that writes to std::basic_streambuf (class template) |
CharTraits is satisfied by the following standard library classes:
template<> class char_traits<char>; template<> class char_traits<wchar_t>; template<> class char_traits<char8_t>; template<> class char_traits<char16_t>; template<> class char_traits<char32_t>; | (since C++20) (since C++11) (since C++11) |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 335 | C++98 | the requirements on the binary overload ofassign did not prevent assignments to rvalues | its first argument can only be an lvalue |
LWG 352 | C++98 | X::state_type was onlyrequired to be CopyConstructible | it is also required to be CopyAssignable and DefaultConstructible |
LWG 3085 | C++98 | X::copy only required p not in[ s , s + n ) , which is too weak[1] | requires [ p , p + n ) and[ s , s + n ) not to overlap |
[
p
,
p + n
)
and [
s
,
s + n
)
can overlap, using std::memcpy
to implement X::copy
results in undefined behavior in this case.
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/named_req/CharTraits