Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |locale‎ |ctype
       
       
       
      Localization library
       
       
      Defined in header<locale>
      public:
      char narrow( CharT c,char dflt)const;
      (1)
      public:

      const CharT* narrow(const CharT* beg,const CharT* end,

                           char dflt,char* dst)const;
      (2)
      protected:
      virtualchar do_narrow( CharT c,char dflt)const;
      (3)
      protected:

      virtualconst CharT* do_narrow(const CharT* beg,const CharT* end,

                                     char dflt,char* dst)const;
      (4)
      1,2) Public member function, calls the corresponding protected virtual member functiondo_narrow overload of the most derived class. Overload (1) callsdo_narrow(c, dflt), overload (2) callsdo_narrow(beg, end, dflt, dst).
      3) Converts the (possibly wide) characterc to multibyte representation if the character can be represented with a single byte (for example, ASCII characters in UTF-8 encoding are single bytes). Returnsdflt if such conversion does not exist.
      4) For every character in the character array[begend), writes narrowed characters (ordflt whenever narrowing fails) to the successive locations in the character array pointed to bydst.

      Narrowing is always successful and is always reversible (by callingwiden()) for all characters from thebasic source character set(until C++23)basic character set(since C++23).

      Narrowing, if successful, preserves all character classification categories known tois().

      • i.e.is(m, c)||!ctc.is(m, do_narrow(c, dflt)) is alwaystrue for any namedctype category with actype<char> facetctc andctype_base::mask valuem (unlessdo_narrow returnsdflt).

      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.

      • i.e. for any digit characterc, the expression(do_narrow(c, dflt)-'0') evaluates to the digit value of the character.

      Contents

      [edit]Parameters

      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

      [edit]Return value

      1,3) Narrowed character ordflt if narrowing fails.
      2,4)end

      [edit]Example

      Run this code
      #include <iostream>#include <locale> void try_narrow(conststd::ctype<wchar_t>& f,wchar_t c){char n= f.narrow(c,0);if(n)std::wcout<<'\''<< c<<"' narrowed to "<<+(unsignedchar)n<<'\n';elsestd::wcout<<'\''<< c<<"' could not be narrowed\n";} int main(){std::locale::global(std::locale("en_US.utf8"));std::wcout.imbue(std::locale());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'ě'); std::locale::global(std::locale("cs_CZ.iso88592"));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'ě');}

      Possible output:

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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 126C++981. the code representing reversibility was
      do_widen(do_narrow(c),0)== c
      2. the code representing category preservation was
      is(m, c)||!ctc.is(m, do_narrow(c), dflt)
      corrected both
      LWG 153C++98narrow always called overload (4)calls the corresponding overload

      [edit]See also

      invokesdo_widen
      (public member function)[edit]
      narrows characters
      (public member function ofstd::basic_ios<CharT,Traits>)[edit]
      narrows a wide character to a single-byte narrow character, if possible
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/ctype/narrow&oldid=169296"

      [8]ページ先頭

      ©2009-2025 Movatter.jp