Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_ostream<CharT,Traits>::operator<<

      From cppreference.com
      <cpp‎ |io‎ |basic ostream
       
       
       
       
      basic_ostream& operator<<(bool value);
      (1)
      basic_ostream& operator<<(long value);
      (2)
      basic_ostream& operator<<(unsignedlong value);
      (3)
      basic_ostream& operator<<(longlong value);
      (4)(since C++11)
      basic_ostream& operator<<(unsignedlonglong value);
      (5)(since C++11)
      basic_ostream& operator<<(double value);
      (6)
      basic_ostream& operator<<(longdouble value);
      (7)
      basic_ostream& operator<<(constvoid* value);
      (8)
      basic_ostream& operator<<(constvolatilevoid* value);
      (9)(since C++23)
      basic_ostream& operator<<(std::nullptr_t);
      (10)(since C++17)
      basic_ostream& operator<<(short value);
      (11)
      basic_ostream& operator<<(int value);
      (12)
      basic_ostream& operator<<(unsignedshort value);
      (13)
      basic_ostream& operator<<(unsignedint value);
      (14)
      basic_ostream& operator<<(float value);
      (15)
      basic_ostream& operator<<(/* extended-floating-point-type */ value);
      (16)(since C++23)
      basic_ostream& operator<<(std::basic_streambuf<CharT, Traits>* sb);
      (17)
      basic_ostream& operator<<(
         std::ios_base&(*func)(std::ios_base&));
      (18)
      basic_ostream& operator<<(
         std::basic_ios<CharT, Traits>&(*func)(std::basic_ios<CharT, Traits>&));
      (19)
      basic_ostream& operator<<(

         std::basic_ostream<CharT, Traits>&(*func)

             (std::basic_ostream<CharT, Traits>&));
      (20)

      Inserts data into the stream.

      1-8) Insertsvalue.
      This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, inserts a value by callingstd::num_put::put(). If the end of file condition was encountered during output (put().failed()==true), setsbadbit.
      9) Equivalent toreturn operator<<(const_cast<constvoid*>(p));.
      10) Equivalent toreturn*this<< s;, wheres is an implementation-defined null-terminated character type string.
      11) Inserts a value fromshortvalue.
      This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, inserts along valuelval as in(2), wherelval is
      12) Inserts a value fromintvalue.
      This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, inserts along valuelval as in(2), wherelval is
      13,14) Inserts a value fromunsignedshort orunsignedintvalue.
      This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, insertsstatic_cast<unsignedlong>(value) as in(3).
      15) Inserts a value fromfloatvalue.
      This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, insertsstatic_cast<double>(value) as in(6).
      16) Inserts a value fromvalue. The library provides overloads for all cv-unqualifiedextended floating-point types as the type of the parameter value.
      This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, checks thefloating-point conversion rank of/* extended-floating-point-type */:
      • If the rank is less than or equal to that ofdouble, insertsstatic_cast<double>(value) as in(6).
      • Otherwise, if the rank is less than or equal to that oflongdouble, insertsstatic_cast<longdouble>(value) as in(7).
      • Otherwise, an invocation of this overload is conditionally supported with implementation-defined semantics.
      17) This function behaves as anUnformattedOutputFunction. After constructing and checking the sentry object, checks ifsb is a null pointer. If it is, executessetstate(badbit) and exits. Otherwise, extracts characters from the input sequence controlled bysb and inserts them into*this until one of the following conditions are met:
      • end-of-file occurs on the input sequence;
      • inserting in the output sequence fails (in which case the character to be inserted is not extracted);
      • an exception occurs (in which case the exception is caught).
      If no characters were inserted, executessetstate(failbit). If an exception was thrown while extracting, setsfailbit and, iffailbit is set inexceptions(), rethrows the exception.
      18-20) Callsfunc(*this). These overloads are used to implement output I/O manipulators such asstd::endl.

      Contents

      [edit]Parameters

      value - integer, floating-point, boolean, or pointer value to insert
      func - function to call
      sb - pointer to the stream buffer to read the data from

      [edit]Return value

      1-19)*this
      20)func(*this)

      [edit]Notes

      There are no overloads for pointers to non-static members, pointers to volatiles,(until C++23) or function pointers (other than the ones with signatures accepted by the(18-20) overloads).

      • Attempting to output such objects invokes implicit conversion tobool, and, for any non-null pointer value, the value1 is printed (unlessboolalpha was set, in which casetrue is printed).

      Character and character string arguments (e.g., of typechar orconstchar*) are handled by thenon-member overloads ofoperator<<.

      • Attempting to output a character using the member function call syntax (e.g.,std::cout.operator<<('c');) will call one of the overloads in(2-5) or(11-14) and output the numerical value.
      • Attempting to output a character string using the member function call syntax will call overload(8) and print the pointer value instead.

      Overload(10) was added by the resolution ofLWG issue 2221, but it is never implemented in any standard library implementation under C++11/14 modes.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <sstream> int fun(){return42;} int main(){std::istringstream input("\"Some text.\" ");double f=3.14;bool b=true; std::cout<< fun()// int overload (12)<<' '// non-member overload<<std::boolalpha// function overload (18)<< b// bool overload (1)<<" "// non-member overload<<std::fixed// function overload (18) again<< f// double overload (6)<< input.rdbuf()// streambuf overload<< fun// bool overload (1): there's no overload for int(*)()<<std::endl;// function overload (18) again int x=0;int* p1=&x;volatileint* p2=&x;std::cout<<"p1: "<< p1<<'\n'// `const void*` overload, prints address<<"p2: "<< p2<<'\n';// before C++23 (P1147): bool overload :), because// operator<<(const void*) is not a match, as it discards the `volatile`// qualifier. To fix this, C++23 adds `const volatile void*` overload (9),// that prints the address as expected.}

      Possible output:

      42 true 3.140000 "Some text." truep1: 0x7ffcea766600p2: 0x7ffcea766600

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 117C++98overloads(1-8,11-15) delegated the insertion to
      num_put::put, but it does not have overloads forshort,
      unsignedshort,int,unsignedint, andfloat
      they are converted
      before being passed
      tonum_put::put
      LWG 567C++98overload(17) behaved as aFormattedOutputFunction
      because of the resolution ofLWG issue 60
      it behaves as an
      UnformattedOutputFunction

      [edit]See also

      inserts character data or insert into rvalue stream
      (function template)[edit]
      performs stream input and output on strings
      (function template)[edit]
      (C++17)
      performs stream output on string views
      (function template)[edit]
      performs stream input and output of bitsets
      (function template)[edit]
      serializes and deserializes a complex number
      (function template)[edit]
      performs stream input and output on pseudo-random number engine
      (function template)[edit]
      performs stream input and output on pseudo-random number distribution
      (function template)[edit]
      inserts a character
      (public member function)[edit]
      inserts blocks of characters
      (public member function)[edit]
      (C++17)
      converts an integer or floating-point value to a character sequence
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/operator_ltlt&oldid=179588"

      [8]ページ先頭

      ©2009-2025 Movatter.jp