std::basic_ostream<CharT,Traits>::operator<<
| basic_ostream& operator<<( short value ); basic_ostream& operator<<( unsigned short value ); |
(1) | |
| basic_ostream& operator<<( int value ); basic_ostream& operator<<( unsigned int value ); |
(2) | |
| basic_ostream& operator<<( long value ); basic_ostream& operator<<( unsigned long value ); |
(3) | |
| basic_ostream& operator<<( long long value ); basic_ostream& operator<<( unsigned long long value ); |
(4) | (since C++11) |
| basic_ostream& operator<<( float value ); basic_ostream& operator<<( double value ); |
(5) | |
| 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>* sb ); |
(10) | |
| basic_ostream& operator<<( std::ios_base& (*func)(std::ios_base&) ); |
(11) | |
| basic_ostream& operator<<( std::basic_ios<CharT, Traits>& (*func)(std::basic_ios<CharT, Traits>&) ); |
(12) | |
| basic_ostream& operator<<( std::basic_ostream<CharT, Traits>& (*func) |
(13) | |
Inserts data into the stream.
badbit.- 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).
failbit and, if failbit is set in exceptions(), rethrows the exception.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
[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 (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.
[edit] Example
#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 (2) << ' ' // non-member overload << std::boolalpha // function overload (11) << b // bool overload (6) << " " // non-member overload << std::fixed // function overload (11) again << f // double overload (5) << input.rdbuf() // streambuf overload << fun // bool overload (6): there's no overload for int(*)() << std::endl; // function overload (11) 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, // that prints the address as expected. }
Possible output:
42 true 3.140000 "Some text." true p1: 0x7ffcea766600 p2: 0x7ffcea766600
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 117 | C++98 | overloads (1-7) delegated the insertion to num_put::put, but it does not have overloads for short, unsigned short, int, unsigned int, and float |
they are converted before being passed to num_put::put
|
| LWG 567 | C++98 | overload (10) behaved as a FormattedOutputFunction because of the resolution of LWG issue 60 |
it behaves as an UnformattedOutputFunction |
[edit] See also
| inserts character data or insert into rvalue stream (function template) | |
| performs stream input and output on strings (function template) | |
| (C++17) |
performs stream output on string views (function template) |
| performs stream input and output of bitsets (function template) | |
| serializes and deserializes a complex number (function template) | |
| (C++11) |
performs stream input and output on pseudo-random number engine (function template) |
| (C++11) |
performs stream input and output on pseudo-random number distribution (function template) |
| inserts a character (public member function) | |
| inserts blocks of characters (public member function) | |
| (C++17) |
converts an integer or floating-point value to a character sequence (function) |