Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator<<(std::basic_ostream)

      From cppreference.com
      <cpp‎ |io‎ |basic ostream
       
       
       
       
      Defined in header<ostream>
      basic_ostream and character
      (1)
      template<class CharT,class Traits>

      basic_ostream<CharT, Traits>&

          operator<<( basic_ostream<CharT, Traits>& os, CharT ch);
      template<class CharT,class Traits>

      basic_ostream<CharT, Traits>&

          operator<<( basic_ostream<CharT, Traits>& os,char ch);
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,char ch);
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,signedchar ch);
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,unsignedchar ch);
      basic_ostream and character array
      (2)
      template<class CharT,class Traits>

      basic_ostream<CharT, Traits>&

          operator<<( basic_ostream<CharT, Traits>& os,const CharT* s);
      template<class CharT,class Traits>

      basic_ostream<CharT, Traits>&

          operator<<( basic_ostream<CharT, Traits>& os,constchar* s);
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,constchar* s);
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,constsignedchar* s);
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,constunsignedchar* s);
      basic_ostream rvalue
      template<class Ostream,class T>
      Ostream&& operator<<( Ostream&& os,const T& value);
      (3)(since C++11)
      deleted overloads for basic_ostream and UTF character/array
      (4)(since C++20)
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,wchar_t ch)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os, char8_t ch)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,char16_t ch)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,char32_t ch)= delete;
      template<class Traits>

      basic_ostream<wchar_t, Traits>&

          operator<<( basic_ostream<wchar_t, Traits>& os, char8_t ch)= delete;
      template<class Traits>

      basic_ostream<wchar_t, Traits>&

          operator<<( basic_ostream<wchar_t, Traits>& os,char16_t ch)= delete;
      template<class Traits>

      basic_ostream<wchar_t, Traits>&

          operator<<( basic_ostream<wchar_t, Traits>& os,char32_t ch)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,constwchar_t* s)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,const char8_t* s)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,constchar16_t* s)= delete;
      template<class Traits>

      basic_ostream<char, Traits>&

          operator<<( basic_ostream<char, Traits>& os,constchar32_t* s)= delete;
      template<class Traits>

      basic_ostream<wchar_t, Traits>&

          operator<<( basic_ostream<wchar_t, Traits>& os,const char8_t* s)= delete;
      template<class Traits>

      basic_ostream<wchar_t, Traits>&

          operator<<( basic_ostream<wchar_t, Traits>& os,constchar16_t* s)= delete;
      template<class Traits>

      basic_ostream<wchar_t, Traits>&

          operator<<( basic_ostream<wchar_t, Traits>& os,constchar32_t* s)= delete;

      Inserts a character or a character string.

      1) Behaves as aFormattedOutputFunction. After constructing and checking thesentry object, inserts the characterch. Ifch has typechar and thecharacter container type ofos is notchar,os.widen(ch) will be inserted instead.
      Padding is determined as follows:
      • Ifos.width()>1, thenos.width()-1 copies ofos.fill() are added to the output character to form the output character sequence.
      • If(out.flags()&std::ios_base::adjustfield)==std::ios_base::left, the fill characters are placed after the output character, otherwise before.
      After insertion,os.width(0) is called to cancel the effects ofstd::setw, if any.
      2) Behaves as aFormattedOutputFunction. After constructing and checking the sentry object, inserts successive characters from the character array whose first element is pointed to bys.
      • For the first and third overloads (whereCharT matches the type ofch), exactlytraits::length(s) characters are inserted.
      • For the second overload, exactlystd::char_traits<char>::length(s) characters are inserted.
      • For the last two overloads, exactlytraits::length(reinterpret_cast<constchar*>(s)) are inserted.
      Before insertion, first, all characters are widened usingos.widen(), then padding is determined as follows:
      • If the number of characters to insert is less thanos.width(), then enough copies ofos.fill() are added to the character sequence to make its length equalos.width().
      • If(out.flags()&std::ios_base::adjustfield)==std::ios_base::left, the fill characters are added at the end of the output sequence, otherwise they are added before the output sequence.
      After insertion,os.width(0) is called to cancel the effects ofstd::setw, if any.
      Ifs is a null pointer, the behavior is undefined.
      3) Calls the appropriate insertion operator, given an rvalue reference to an output stream object (equivalent toos<< value). This overload participates in overload resolution only if the expressionos<< value is well-formed andOstream is a class type publicly and unambiguously derived fromstd::ios_base.
      4) Overloads that acceptchar16_t,char32_t etc (or null terminated sequence thereof) are deleted:std::cout<< u'X' is not allowed. Previously, these would print an integer or pointer value.

      Contents

      [edit]Parameters

      os - output stream to insert data to
      ch - reference to a character to insert
      s - pointer to a character string to insert

      [edit]Return value

      1,2)os
      3)std::move(os)

      [edit]Notes

      BeforeLWG issue 1203, code such as(std::ostringstream()<<1.2).str() does not compile.

      [edit]Example

      Run this code
      #include <fstream>#include <iostream> void foo(){// error: operator<< (basic_ostream<char, _Traits>&, char8_t) is deleted//  std::cout << u8'z' << '\n';} std::ostream& operator<<(std::ostream& os, char8_tconst& ch){return os<<static_cast<char>(ch);} int main(){std::cout<<"Hello, world"// uses `const char*` overload<<'\n';// uses `char` overloadstd::ofstream{"test.txt"}<<1.2;// uses rvalue overload std::cout<< u8'!'<<'\n';// uses program-defined operator<<(os, char8_t const&)}

      Output:

      Hello, world!

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 167C++98the number of characters inserted for all
      overloads in(2) wastraits::length(s)
      updated the numbers for the overloads
      whereCharT does not match the type ofch
      LWG 1203C++11overload for rvalue stream returned
      lvalue reference to the base class
      returns rvalue reference
      to the derived class
      LWG 2011C++98padding was determined bystd::num_put::do_put()determined by the operator itself
      LWG 2534C++11overload for rvalue stream was not constrainedconstrained

      [edit]See also

      inserts formatted data
      (public member function)[edit]
      outputsformatted representation of the arguments
      (function template)[edit]
      widens characters
      (public member function ofstd::basic_ios<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/operator_ltlt2&oldid=169503"

      [8]ページ先頭

      ©2009-2025 Movatter.jp