std::ctype<CharT>::narrow,do_narrow (3) - Linux Man Pages

std::ctype<CharT>::narrow,do_narrow: std::ctype<CharT>::narrow,do_narrow


std::ctype<CharT>::narrow,do_narrow - std::ctype<CharT>::narrow,do_narrow


Defined in header <locale>
public: (1)
char narrow( CharT c, char dflt ) const;
const CharT* narrow( const CharT* beg, const CharT* end, (2)
char dflt, char* dst ) const;
protected: (3)
virtual char do_narrow( CharT c, char dflt ) const;
virtual const CharT* do_narrow( const CharT* beg, const CharT* end, (4)
char dflt, char* dst ) const;

1,2) Public member function, calls the protected virtual member function do_narrow of the most derived class.
3) Converts the (possibly wide) character c to multibyte representation if the character can be represented with a single byte (for example, ASCII characters in UTF-8 encoding are single bytes). Returns dflt if such conversion does not exist.
4) For every character in the character array [beg, end), writes narrowed characters (or dflt whenever narrowing fails) to the successive locations in the character array pointed to by dst.
Narrowing is always successful and is always reversible (by calling widen()) for all characters from the basic source character set (latin letters, digits, and punctuations required to write a C++ program)
Narrowing, if successful, preserves all character classification categories known to is().
Narrowing of any digit character guarantees that if the result is subtracted from the character literal '0', the difference equals the digit value of the original character.


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 char to fill

Return value

1,3) narrowed character or dflt if narrowing fails
2,4) end


// Run this code

  #include <locale>
  #include <iostream>

  void try_narrow(const std::ctype<wchar_t>& f, wchar_t c)
      char n = f.narrow(c, 0);
      if (n) {
          std::wcout << '\'' << c << "' narrowed to " << +(unsigned char)n << '\n';
      } else {
          std::wcout << '\'' << c << "' could not be narrowed\n";

  int main()
      std::wcout << std::hex << std::showbase << "In US English UTF-8 locale:\n";
      auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
      try_narrow(f, L'A');
      try_narrow(f, L'A');
      try_narrow(f, L'ě');

      auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale());
      std::wcout << "In Czech ISO-8859-2 locale:\n";
      try_narrow(f2, L'A');
      try_narrow(f2, L'A');
      try_narrow(f2, L'ě');


  In US English UTF-8 locale:
  'A' narrowed to 0x41
  'A' could not be narrowed
  'ě' could not be narrowed
  In Czech ISO-8859-2 locale:
  'A' narrowed to 0x41
  'A' could not be narrowed
  'ě' narrowed to 0xec

See also

       invokes do_widen
widen (public member function)
       narrows characters
narrow (public member function of std::basic_ios<CharT,Traits>)
       narrows a wide character to a single-byte narrow character, if possible
wctob (function)