std::char_traits - cppreference.com

From cppreference.com

template<classCharT>classchar_traits;The char_traits class is a traits class template that abstracts basic character and string operations for a given character type. The defined operation set is such that generic algorithms almost always can be implemented in terms of it. It is thus possible to use such algorithms with almost any possible character or string type, just by supplying a customized char_traits class.

The char_traits class template serves as a basis for explicit instantiations. The user can

provide a specialization

for any custom character types. Several explicit specializations are provided for the standard character types (see below), other specializations are not required to satisfy the requirements of

CharTraits

.

Specializations

The standard library provides the following standard specializations:

Defined in header

<string>

std::char_traits<char>the standard character traits of charstd::char_traits<wchar_t>the standard character traits of wchar_tstd::char_traits<char8_t>(C++20)the standard character traits of char8_tstd::char_traits<char16_t>(C++11)the standard character traits of char16_tstd::char_traits<char32_t>(C++11)the standard character traits of char32_tAll these specializations satisfy the requirements of

CharTraits

.

Member types

The standard specializations define the following member types required by

CharTraits

:

CharTMember type char_typeint_typeoff_typepos_typestate_typecharcharint

std::streamoff

std::streampos

std::mbstate_t

wchar_twchar_t

std::wint_t

std::wstreampos

char8_tchar8_tunsignedint

std::u8streampos

char16_tchar16_t

std::uint_least16_t

std::u16streampos

char32_tchar32_t

std::uint_least32_t

std::u32streampos

On top of that, the standard specializations also define the member type comparison_category as

std::strong_ordering

.

(since C++20)Member functions

The standard specializations define the following static member functions required by

CharTraits

:

assign

[static]

assigns a character
(public static member function)

[edit]

eqlt

[static]

compares two characters
(public static member function)

[edit]

move

[static]

moves one character sequence onto another
(public static member function)

[edit]

copy

[static]

copies a character sequence
(public static member function)

[edit]

compare

[static]

lexicographically compares two character sequences
(public static member function)

[edit]

length

[static]

returns the length of a character sequence
(public static member function)

[edit]

find

[static]

finds a character in a character sequence
(public static member function)

[edit]

to_char_type

[static]

converts int_type to equivalent char_type
(public static member function)

[edit]

to_int_type

[static]

converts char_type to equivalent int_type
(public static member function)

[edit]

eq_int_type

[static]

compares two int_type values
(public static member function)

[edit]

eof

[static]

returns an eof value
(public static member function)

[edit]

not_eof

[static]

checks whether a character is eof value
(public static member function)

[edit]

Notes

CharTraits

does not require defining the types and functions listed above as direct members, it only requires types like X::type and expressions like X::func(args) are valid and have the required semantics. Users-defined character traits can be derived from other character traits classes and only override some of their members, see the example below.

Example

User-defined character traits may be used to provide

case-insensitive comparison

:

Run this code

#include<cctype>#include<iostream>#include<string>#include<string_view>structci_char_traits:publicstd::char_traits<char>{staticcharto_upper(charch){returnstd::toupper((unsignedchar)ch);}staticbooleq(charc1,charc2){returnto_upper(c1)==to_upper(c2);}staticboollt(charc1,charc2){returnto_upper(c1)<to_upper(c2);}staticintcompare(constchar*s1,constchar*s2,std::size_tn){while(n--!=0){if(to_upper(*s1)<to_upper(*s2))return-1;if(to_upper(*s1)>to_upper(*s2))return1;++s1;++s2;}return0;}staticconstchar*find(constchar*s,std::size_tn,chara){constautoua{to_upper(a)};while(n--!=0){if(to_upper(*s)==ua)returns;s++;}returnnullptr;}};template<classDstTraits,classCharT,classSrcTraits>constexprstd::basic_string_view<CharT,DstTraits>traits_cast(conststd::basic_string_view<CharT,SrcTraits>src)noexcept{return{src.data(),src.size()};}intmain(){usingnamespacestd::literals;constexprautos1="Hello"sv;constexprautos2="heLLo"sv;if(traits_cast<ci_char_traits>(s1)==traits_cast<ci_char_traits>(s2))std::cout<<s1<<" and "<<s2<<" are equal\n";}Output:

Hello and heLLo are equal See also