std::codecvt (3) - Linux Manuals
std::codecvt: std::codecvt
NAME
Synopsis
Defined in header <locale>
template<
class InternT,
class ExternT,
class State
> class codecvt;
Class std::codecvt encapsulates conversion of character strings, including wide and multibyte, from one encoding to another. All file I/O operations performed through std::basic_fstream<CharT> use the std::codecvt<CharT, char, std::mbstate_t> facet of the locale imbued in the stream.
std-codecvt-inheritance.svg
Inheritance diagram
The following standalone (locale-independent) specializations are provided by the standard library:
Defined in header <locale>
std::codecvt<char, char, std::mbstate_t> identity conversion
std::codecvt<char16_t, char, std::mbstate_t> conversion between UTF-16 and UTF-8 (since C++11)(deprecated in C++20)
std::codecvt<char16_t, char8_t, std::mbstate_t> conversion between UTF-16 and UTF-8 (since C++20)
std::codecvt<char32_t, char, std::mbstate_t> conversion between UTF-32 and UTF-8 (since C++11)(deprecated in C++20)
std::codecvt<char32_t, char8_t, std::mbstate_t> conversion between UTF-32 and UTF-8 (since C++20)
std::codecvt<wchar_t, char, std::mbstate_t> conversion between the system's native wide and the single-byte narrow character sets
In addition, every locale object constructed in a C++ program implements its own (locale-specific) versions of these four specializations.
Member types
Member type Definition
intern_type InternT
extern_type ExternT
state_type State
Member functions
constructor (public member function)
destructor (protected member function)
out (public member function)
in (public member function)
unshift (public member function)
encoding (public member function)
always_noconv (public member function)
length (public member function)
max_length (public member function)
Member objects
Member name Type
id (static) std::locale::id
Protected member functions
do_out converts a string from internT to externT, such as when writing to file
[virtual]
do_in converts a string from externT to internT, such as when reading from file
[virtual]
do_unshift generates the termination character sequence of externT characters for incomplete conversion
[virtual]
do_encoding returns the number of externT characters necessary to produce one internT character, if constant
[virtual]
do_always_noconv tests if the facet encodes an identity conversion for all valid argument values
[virtual]
do_length calculates the length of the externT string that would be consumed by conversion into given internT buffer
[virtual]
do_max_length returns the maximum number of externT characters that could be converted into a single internT character
[virtual]
Inherited from std::codecvt_base
Member type Definition
enum result { ok, partial, error, noconv }; Unscoped enumeration type
Enumeration constant Definition
ok conversion was completed with no error
partial not all source characters were converted
error encountered an invalid character
noconv no conversion required, input and output types are the same
Example
The following examples reads a UTF-8 file using a locale which implements UTF-8 conversion in codecvt<wchar_t, char, mbstate_t> and converts a UTF-8 string to UTF-16 using one of the standard specializations of std::codecvt
// Run this code