| Localization library | |||||||||||||||||||||||||
| Regular expressions library(C++11) | |||||||||||||||||||||||||
| Formatting library(C++20) | |||||||||||||||||||||||||
| Null-terminated sequence utilities | |||||||||||||||||||||||||
| Byte strings | |||||||||||||||||||||||||
| Multibyte strings | |||||||||||||||||||||||||
| Wide strings | |||||||||||||||||||||||||
| Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| Text encoding identifications | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
ctype::widenctype::do_widen | ||||
| Member functions of ctype<char> | ||||
Defined in header <locale> | ||
public: CharT widen(char c)const; | (1) | |
public: constchar* widen(constchar* beg,constchar* end, CharT* dst)const; | (2) | |
protected: virtual CharT do_widen(char c)const; | (3) | |
protected: virtualconstchar* do_widen(constchar* beg,constchar* end, CharT* dst)const; | (4) | |
do_widen overload of the most derived class. Overload (1) callsdo_widen(c), overload (2) callsdo_widen(beg, end, dst).[beg, end), writes the corresponding widened character to the successive locations in the character array pointed to bydst.Widening always returns a wide character, but only the characters from thebasic source character set(until C++23)basic character set(since C++23) are guaranteed to have a unique, well-defined, widening transformation, which is also guaranteed to be reversible (bynarrow()). In practice, all characters whose multibyte representation is a single byte are usually widened to their wide character counterparts, and the rest of the possible single-byte values are usually mapped into the same placeholder value, typicallyCharT(-1).
Widening, if successful, preserves all character classification categories known tois().
Contents |
| c | - | character to convert |
| dflt | - | default value to produce if the conversion fails |
| beg | - | pointer to the first character in an array of characters to convert |
| end | - | one past the end pointer for the array of characters to convert |
| dst | - | pointer to the first element of the array of characters to fill |
#include <iostream>#include <locale> void try_widen(conststd::ctype<wchar_t>& f,char c){wchar_t w= f.widen(c);std::cout<<"The single-byte character "<<+(unsignedchar)c<<" widens to "<<+w<<'\n';} int main(){std::locale::global(std::locale("cs_CZ.iso88592"));auto& f=std::use_facet<std::ctype<wchar_t>>(std::locale());std::cout<<std::hex<<std::showbase<<"In Czech ISO-8859-2 locale:\n"; try_widen(f,'a'); try_widen(f,'\xdf');// German letter ß (U+00df) in ISO-8859-2 try_widen(f,'\xec');// Czech letter ě (U+011b) in ISO-8859-2 std::locale::global(std::locale("cs_CZ.utf8"));auto& f2=std::use_facet<std::ctype<wchar_t>>(std::locale());std::cout<<"In Czech UTF-8 locale:\n"; try_widen(f2,'a'); try_widen(f2,'\xdf'); try_widen(f2,'\xec');}
Possible output:
In Czech ISO-8859-2 locale:The single-byte character 0x61 widens to 0x61The single-byte character 0xdf widens to 0xdfThe single-byte character 0xec widens to 0x11bIn Czech UTF-8 locale:The single-byte character 0x61 widens to 0x61The single-byte character 0xdf widens to 0xffffffffThe single-byte character 0xec widens to 0xffffffff
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 153 | C++98 | widen always called overload (4) | calls the corresponding overload |
invokesdo_narrow(public member function)[edit] | |
| widens characters (public member function of std::basic_ios<CharT,Traits>)[edit] | |
| widens a single-byte narrow character to wide character, if possible (function)[edit] |