std::basic_...:operator<<(3)
okay so bestie, welcome to the std::basic_...:operator<<(3) page from the C++ Standard Libary β yes, that's the header vibe, and we're folding it in right here instead of copying the line π
πΈ NAME πΈ
std::basic_ostream::operator<< β std::basic_ostream::operator<< (the name is the name, we move π)
β¨ Synopsis β¨
basic_ostream& operator<<( bool value ); (1)
basic_ostream& operator<<( long value ); (2)
basic_ostream& operator<<( unsigned long value ); (3)
basic_ostream& operator<<( long long value ); (4) (since C++11)
basic_ostream& operator<<( unsigned long long value ); (5) (since C++11)
basic_ostream& operator<<( double value ); (6)
basic_ostream& operator<<( long double value ); (7)
basic_ostream& operator<<( const void* value ); (8)
basic_ostream& operator<<( const volatile void* 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<<( unsigned short value ); (13)
basic_ostream& operator<<( unsigned int 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)
π DESCRIPTION π
Inserts data into the stream. Literally the whole gig β¨
- 1-8) Inserts
value. 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 to
return operator<<(const_cast<const void*>(p));. - 10) Equivalent to
return *this << s;, wheresis an implementation-defined null-terminated character type string. - 11) Inserts a value from
short value. This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, inserts along value lvalas in (2), wherelvalis:static_cast<long>(static_cast<unsigned short>(value)), ifflags() & std::ios_base::basefieldisstd::ios_base::octorstd::ios_base::hex, orstatic_cast<long>(value)otheriwse (their spelling, not mine π).
- 12) Inserts a value from
int value. This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, inserts along value lvalas in (2), wherelvalis:static_cast<long>(static_cast<unsigned int>(value)), ifflags() & std::ios_base::basefieldisstd::ios_base::octorstd::ios_base::hex, orstatic_cast<long>(value)otheriwse.
- 13,14) Inserts a value from
unsigned shortorunsigned int value. This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, insertsstatic_cast<unsigned long>(value)as in (3). - 15) Inserts a value from
float value. This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, insertsstatic_cast<double>(value)as in (6). - 16) Inserts a value from
value. The library provides overloads for all cv-unqualified extended floating-point types as the type of the parametervalue. This function behaves as aFormattedOutputFunction. After constructing and checking the sentry object, checks the floating-point conversion rank of/* extended-floating-point-type */:- If the rank is less than or equal to that of
double, insertsstatic_cast<double>(value)as in (6). - Otherwise, if the rank is less than or equal to that of
long double, insertsstatic_cast<long double>(value)as in (7). - Otherwise, an invocation of this overload is conditionally supported with implementation-defined semantics.
- If the rank is less than or equal to that of
- 17) This function behaves as an
UnformattedOutputFunction. After constructing and checking the sentry object, checks ifsbis a null pointer. If it is, executessetstate(badbit)and exits. Otherwise, extracts characters from the input sequence controlled bysband inserts them into*thisuntil 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.
- 18-20) Calls
func(*this). These overloads are used to implement output I/O manipulators such asstd::endl.
π Parameters π
valueβ integer, floating-point, boolean, or pointer value to insertfuncβ function to callsbβ pointer to the stream buffer to read the data from
π¦ Return value π¦
- 1-19)
*this - 20)
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 (18-20) overloads).
- Attempting to output such objects invokes implicit conversion to
bool, and, for any non-null pointer value, the value1is printed (unlessboolalphawas set, in which casetrueis 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 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.
π Example π
Run this code, bestie:
#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.
}
π Possible output π
42 true 3.140000 "Some text." true
p1: 0x7ffcea766600
p2: 0x7ffcea766600
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR LWG 117 β Applied to: C++98. Behavior as published: overloads (1-8,11-15) delegated the insertion to
num_put::put, but it does not have overloads forshort,unsigned short,int,unsigned int, andfloat. Correct behavior: they are converted before being passed tonum_put::put. - DR LWG 567 β Applied to: C++98. Behavior as published: overload (17) behaved as a
FormattedOutputFunctionbecause of the resolution of LWG issue 60. Correct behavior: it behaves as anUnformattedOutputFunction.
π See also π
operator<<(std::basic_ostream)β inserts character data or insert into rvalue stream (function template)operator<<,operator>>β performs stream input and output on strings (function template)operator<<β performs stream output on string views (C++17) (function template)operator<<,operator>>β performs stream input and output of bitsets (function template)operator<<,operator>>β serializes and deserializes a complex number (function template)operator<<,operator>>β performs stream input and output on pseudo-random number engine (C++11) (function template)operator<<,operator>>β performs stream input and output on pseudo-random 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 character sequence (C++17) (function)
okay that's the whole page!! source is http://cppreference.com, version/date is 2024.06.10, and the manual name is std::basic_...:operator<<(3). slay, go insert some data π
β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms