table of contents
std::basic_ostream::operator<<(3) | C++ Standard Libary | std::basic_ostream::operator<<(3) |
NAME¶
std::basic_ostream::operator<< - std::basic_ostream::operator<<
Synopsis¶
basic_ostream& operator<<( short value ); (1)
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value ); (2)
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value ); (3)
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value ); (4) (since
C++11)
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value ); (5)
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value ); (6)
basic_ostream& operator<<( const void* value ); (7)
basic_ostream& operator<<( const volatile void* value ); (8)
(since C++23)
basic_ostream& operator<<( std::nullptr_t ); (9) (since
C++17)
basic_ostream& operator<<( std::basic_streambuf<CharT,
Traits>* (10)
sb );
basic_ostream& operator<<( (11)
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (12)
(*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (13)
(*func)(std::basic_ostream<CharT,Traits>&) );
Inserts data into the stream.
1-2) Behaves as a FormattedOutputFunction. After constructing and checking
the
sentry object, if value is short or int, then casts it to unsigned short or
unsigned
int if ios_base::flags() & ios_base::basefield is ios_base::oct or
ios_base::hex.
After that, casts to long in any case and outputs as in (3). If value
is unsigned
short or unsigned int, casts to unsigned long and outputs as in (3).
3-7) Behaves as a FormattedOutputFunction. After constructing and checking
the
sentry object, inserts an integer, floating point, boolean or generic pointer
value
by calling num_put::put(). If the end of file condition was encountered
during
output (put().failed() == true), sets ios::badbit.
8) Casts value to const void* and outputs as in (7).
9) Outputs an implementation-defined string as if by *this << s, where
s is a
null-terminated character type string.
10) Behaves as an UnformattedOutputFunction. After constructing and checking
the
sentry object, checks if sb is a null pointer. If it is, executes
setstate(badbit)
and exits. Otherwise, extracts characters from the input sequence controlled
by sb
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, executes setstate(failbit). If an exception
was
thrown while extracting, sets failbit and, if failbit is set in exceptions(),
rethrows the exception.
11-13) Calls func(*this). These overloads are used to implement output I/O
manipulators such as std::endl.
Parameters¶
value - integer, floating-point, boolean, or pointer value to
insert
func - function to call
sb - pointer to the streambuffer to read the data from
Return value¶
1-12) *this
13) func(*this)
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 (11-13) overloads). Attempting to output such objects invokes implicit
conversion to bool, and, for any non-null pointer value, the value 1 is
printed
(unless boolalpha was set, in which case true is printed).
Character and character string arguments (e.g., of type char or const char*)
are
handled by the non-member overloads of operator<<. Attempting to output
a character
using the member function call syntax (e.g.,
std::cout.operator<<('c');) will call
one of overloads (2-4) and output the numerical value. Attempting to output a
character string using the member function call syntax will call overload
(7) and
print the pointer value instead.
Example¶
// Run this code
#include <iostream>
#include <iomanip>
#include <sstream>
int fun() { return 42; }
int main()
{
std::istringstream input(" \"Some text.\" ");
double f = 3.14;
bool b = true;
std::cout
<< fun() // int overload
<< ' ' // non-member overload
<< std::boolalpha << b // bool overload
<< " " // non-member overload
<< std::fixed << f // double overload
<< input.rdbuf() // streambuf overload
<< fun // bool overload: there's no overload for int(*)()
<< std::endl; // function overload
int x{};
int* p1 = &x;
volatile int* 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,
// that prints the address as expected.
}
Possible output:¶
42 true 3.140000 "Some text." true
p1: 0x7ffcea766600
p2: 0x7ffcea766600
See also¶
operator<<(std::basic_ostream) inserts character data or
insert into rvalue stream
(function template)
operator<< performs stream input and output on strings
operator>> (function template)
operator<< performs stream output on string views
(C++17) (function template)
operator<< performs stream input and output of bitsets
operator>> (function template)
operator<< serializes and deserializes a complex number
operator>> (function template)
operator<< performs stream input and output on pseudo-random
operator>> number engine
(C++11) (function template)
operator<< performs stream input and output on pseudo-random
operator>> number distribution
(C++11) (function template)
put inserts a character
(public member function)
write inserts blocks of characters
(public member function)
to_chars converts an integer or floating-point value to a
(C++17) character sequence
(function)
2022.07.31 | http://cppreference.com |