|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 aCharTraits template type parameter alongside a corresponding character template type parameter.
Contents |
No operation listed below onCharTraits may throw an exception.
Given
CharT, a character typeX, aCharTraits type for typeCharTCharTCharT*X::int_typeX::pos_typeX::state_typeCharT| Type | Semantics |
|---|---|
X::char_type | CharT |
X::int_type | A type that can hold all valid values ofX::char_type plusX::eof() |
X::off_type | Invokes implementation-defined behavior if notstd::streamoff whenX is used as the traits template parameter in input/output classes. |
X::pos_type |
|
X::state_type | Destructible,CopyAssignable,CopyConstructible,DefaultConstructible |
| Expression | Return type | Semantics | Complexity |
|---|---|---|---|
| X::eq(c, d) | bool | Returns: whetherc is to be treated as equal tod | Constant |
| X::lt(c, d) | bool | Returns: whetherc is to be treated as less thand | Constant |
| X::compare(p, q, n) | int | Returns:
| Linear |
| X::length(p) | std::size_t | Returns: the smallesti such thatX::eq(p[i], CharT()) istrue | 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) | Assignsr= 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 valuee, constrained by the definitions ofX::to_char_type andX::eq_int_type | Constant |
| X::eq_int_type(e, f) | bool |
| Constant |
| X::eof() | X::int_type | Returns: a valuee such thatX::eq_int_type(e, X::to_int_type(c)) isfalse for all valuesc | 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)[edit] | |
(C++17) | read-only string view (class template)[edit] |
Streams | |
| manages an arbitrary stream buffer (class template)[edit] | |
| wraps a given abstract device (std::basic_streambuf) and provides high-level input interface (class template)[edit] | |
| implements high-level file stream input operations (class template)[edit] | |
| implements high-level string stream input operations (class template)[edit] | |
(C++23) | implements fixed character buffer input operations (class template)[edit] |
| wraps a given abstract device (std::basic_streambuf) and provides high-level output interface (class template)[edit] | |
| implements high-level file stream output operations (class template)[edit] | |
| implements high-level string stream output operations (class template)[edit] | |
(C++20) | synchronized output stream wrapper (class template)[edit] |
(C++23) | implements fixed character buffer output operations (class template)[edit] |
| wraps a given abstract device (std::basic_streambuf) and provides high-level input/output interface (class template)[edit] | |
| implements high-level file stream input/output operations (class template)[edit] | |
| implements high-level string stream input/output operations (class template)[edit] | |
(C++23) | implements fixed character buffer input/output operations (class template)[edit] |
Stream iterators | |
| input iterator that reads fromstd::basic_istream (class template)[edit] | |
| output iterator that writes tostd::basic_ostream (class template)[edit] | |
Stream buffers | |
| abstracts a raw device (class template)[edit] | |
| implements raw file device (class template)[edit] | |
| implements raw string device (class template)[edit] | |
(C++20) | synchronized output device wrapper (class template)[edit] |
(C++23) | implements raw fixed character buffer device (class template)[edit] |
Stream buffer iterators | |
| input iterator that reads fromstd::basic_streambuf (class template)[edit] | |
| output iterator that writes tostd::basic_streambuf (class template)[edit] | |
CharTraits is satisfied by the following standard library explicit specializations ofstd::char_traits:
template<>class char_traits<char>; template<>class char_traits<wchar_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 beCopyConstructible | it is also required to be CopyAssignable andDefaultConstructible |
| LWG 3085 | C++98 | X::copy(s, p, n) only requiredp 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, usingstd::memcpy to implementX::copy results in undefined behavior in this case.