operator<<(std::basic_ostream)
Defined in header <ostream> |
||
---|---|---|
basic_ostream and character | ||
(1) | ||
template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os, CharT ch ); |
||
template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os, char ch ); |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, char ch ); |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, signed char ch ); |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, unsigned char ch ); |
||
basic_ostream and character array | ||
(2) | ||
template< class CharT, class Traits > basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os, const CharT* s ); |
||
template< class CharT, class Traits > basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os, const char* s ); |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const char* s ); |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const signed char* s ); |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const unsigned char* s ); |
||
basic_ostream rvalue | ||
template< class Ostream, class T > Ostream&& operator<<( Ostream&& os, const T& value ); |
(3) | (since C++11) |
deleted overloads for basic_ostream and UTF character/array | ||
(4) | (since C++20) | |
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, wchar_t ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, char8_t ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, char16_t ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, char32_t ch ) = delete; |
||
template< class Traits > basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os, char8_t ch ) = delete; |
||
template< class Traits > basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os, char16_t ch ) = delete; |
||
template< class Traits > basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os, char32_t ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const wchar_t* ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const char8_t* ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const char16_t* ch ) = delete; |
||
template< class Traits > basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os, const char32_t* ch ) = delete; |
||
template< class Traits > basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os, const char8_t* ch ) = delete; |
||
template< class Traits > basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os, const char16_t* ch ) = delete; |
||
template< class Traits > basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os, const char32_t* ch ) = delete; |
Inserts a character or a character string.
ch
. If the type of the character is not
CharT
, it is first converted with
os.widen(ch)
. Padding is determined as follows: if
os.width()>1
, then
os.width()-1
copies of
os.fill()
are added to the output character to form the output character sequence. If
(out.flags()&std::ios_base::adjustfield) == std::ios_base::left
, the fill characters are placed after the output character, otherwise before. After insertion,
os.width(0)
is called to cancel the effects of
std::setw
, if any.
s
.
- for the first and third overloads (where
CharT
matches the type ofch
), exactlytraits::length(s)
characters are inserted. - for the second overload, exactly
std::char_traits<char>::length(s)
characters are inserted. - for the last two overloads, exactly
traits::length(reinterpret_cast<const char*>(s))
are inserted.
Before insertion, first, all characters are widened using os.widen()
, then padding is determined as follows: if the number of characters to insert is less than os.width()
, then enough copies of os.fill()
are added to the character sequence to make its length equal os.width()
. If (out.flags()&std::ios_base::adjustfield) == std::ios_base::left
, the fill characters are added at the end of the output sequence, otherwise they are added before the output sequence. After insertion, width(0)
is called to cancel the effects of std::setw
, if any. The behavior is undefined if s
is a null pointer.
os << value
). This overload participates in overload resolution only if the expression
os << value
is well-formed and
Ostream
is a class type publicly and unambiguously derived from
std::ios_base
.
char16_t
,
char32_t
etc (or null terminated sequence thereof) are deleted:
std::cout << u'X'
is not allowed. Previously, these would print an integer or pointer value.
Parameters
os | - | output stream to insert data to |
ch | - | reference to a character to insert |
s | - | pointer to a character string to insert |
Return value
os
std::move(os)
Notes
Before LWG#1203, code such as (std::ostringstream() << 1.2).str()
does not compile.
Example
#include <iostream> #include <fstream> int main() { std::cout << "Hello, world" // the const char* overload << '\n'; // the char overload std::ofstream("test.txt") << 1.2; // rvalue overload }
Output:
Hello, world
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 1203 | C++11 | overload for rvalue stream returned lvalue reference to the base class | returns rvalue reference to the derived class |
LWG 2534 | C++11 | overload for rvalue stream was not constrained | constrained |
See also
inserts formatted data (public member function) |
|
widens characters (public member function of std::basic_ios<CharT,Traits> ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2