std::basic_ostream<CharT,Traits>::operator<<
Min standard notice:
Inserts data into the stream.
# Declarations
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
(since C++11)
basic_ostream& operator<<( unsigned long long value );
(since C++11)
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( const volatile void* value );
(since C++23)
basic_ostream& operator<<( std::nullptr_t );
(since C++17)
basic_ostream& operator<<( short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( /* extended-floating-point-type */ value );
(since C++23)
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb );
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT, Traits>& (*func)(std::basic_ios<CharT, Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT, Traits>& (*func)
(std::basic_ostream<CharT, Traits>&) );
# Parameters
value: integer, floating-point, boolean, or pointer value to insertfunc: function to callsb: pointer to the stream buffer to read the data from
# 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).
Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator«.
Overload (10) was added by the resolution of LWG issue 2221, but it is never implemented in any standard library implementation under C++11/14 modes.
# Example
#include <iomanip>
#include <iostream>
#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 (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;
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 (9),
// that prints the address as expected.
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 117 | C++98 | overloads (1-8,11-15) delegated the insertion tonum_put::put, but it does not have overloads for short,unsigned short, int, unsigned int, and float | they are convertedbefore being passedto num_put::put |
| LWG 567 | C++98 | overload (17) behaved as a FormattedOutputFunctionbecause of the resolution of LWG issue 60 | it behaves as anUnformattedOutputFunction |