Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::codecvt<InternT,ExternT,StateT>::out, do_out

      From cppreference.com
      <cpp‎ |locale‎ |codecvt
       
       
       
      Localization library
       
       
      Defined in header<locale>
      public:

      result out( StateT& state,
                 const InternT* from,
                 const InternT* from_end,
                 const InternT*& from_next,
                  ExternT* to,
                  ExternT* to_end,

                  ExternT*& to_next)const;
      (1)
      protected:

      virtual result do_out( StateT& state,
                             const InternT* from,
                             const InternT* from_end,
                             const InternT*& from_next,
                             ExternT* to,
                             ExternT* to_end,

                             ExternT*& to_next)const;
      (2)
      1) Public member function, calls the member functiondo_out of the most derived class.
      2) If thiscodecvt facet defines a conversion, translates the internal characters from the source range[fromfrom_end) to external characters, placing the results in the subsequent locations starting atto. Converts no more thanfrom_end- from internal characters and writes no more thanto_end- to external characters. Leavesfrom_next andto_next pointing one beyond the last element successfully converted.

      If thiscodecvt facet does not define a conversion, no characters are converted.to_next is set to be equal toto,state is unchanged, andstd::codecvt_base::noconv is returned.

      do_out(state, from, from+1, from_next, to, to_end, to_next) must returnok if

      • thiscodecvt facet is used bybasic_filebuf, and
      • do_out(state, from, from_end, from_next, to, to_end, to_next) would returnok wherefrom!= from_end.

      Contents

      [edit]Return value

      A value of typestd::codecvt_base::result, indicating the success status as follows:

      ok conversion completed
      partial not enough space in the output buffer or unexpected end of source buffer
      error encountered a character that could not be converted
      noconv this facet is non-converting, no output written

      The non-converting specializationstd::codecvt<char,char,std::mbstate_t> always returnsstd::codecvt_base::noconv.

      [edit]Notes

      Requires thatfrom<= from_end&& to<= to_end and thatstate either representing the initial shift state or obtained by converting the preceding characters in the sequence.

      Whilecodecvt supports N:M conversions (e.g. UTF-16 to UTF-8, where two internal characters may be necessary to decide what external characters to output),std::basic_filebuf can only usecodecvt facets that define a 1:N conversion, that is it must be able to process one internal character at a time when writing to a file.

      When performing N:M conversions, this function may returnstd::codecvt_base::partial after consuming all source characters (from_next== from_end). This means that another internal character is needed to complete the conversion (e.g. when converting UTF-16 to UTF-8, if the last character in the source buffer is a high surrogate).

      The effect onstate is deliberately unspecified. In standard facets, it is used to maintain shift state like when callingstd::wcsrtombs, and is therefore updated to reflect the shift state after the last successfully converted character, but a user-defined facet is free to use it to maintain any other state, e.g. count the number of special characters encountered.

      [edit]Example

      Run this code
      #include <iostream>#include <locale>#include <string> int main(){std::locale::global(std::locale("en_US.utf8"));auto& f=std::use_facet<std::codecvt<wchar_t,char,std::mbstate_t>>(std::locale());std::wstring internal= L"z\u00df\u6c34\U0001f34c";// L"zß水🍌" // note that the following can be done with wstring_convertstd::mbstate_t mb{};// initial shift statestd::string external(internal.size()* f.max_length(),'\0');constwchar_t* from_next;char* to_next;    f.out(mb,&internal[0],&internal[internal.size()], from_next,&external[0],&external[external.size()], to_next);// error checking skipped for brevity    external.resize(to_next-&external[0]); std::cout<<"The string in narrow multibyte encoding: "<< external<<'\n';}

      Output:

      The string in narrow multibyte encoding: zß水🍌

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 76C++98it was unclear whether the conversion is required
      to support taking one internal character at a time
      only required if used
      bybasic_filebuf

      [edit]See also

      [virtual]
      writes characters to the associated file from the put area
      (virtual protected member function ofstd::basic_filebuf<CharT,Traits>)[edit]
      converts a wide string into a byte string
      (public member function ofstd::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>)[edit]
      converts a wide string to narrow multibyte character string, given state
      (function)[edit]
      [virtual]
      converts a string fromExternT toInternT, such as when reading from file
      (virtual protected member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/codecvt/out&oldid=160052"

      [8]ページ先頭

      ©2009-2025 Movatter.jp