operator<<(std::basic_ostream)

From cppreference.com
< cpp‎ | io‎ | basic ostream
 
 
Input/output library
I/O manipulators
C-style I/O
Buffers
(deprecated in C++98)
Streams
Abstractions
File I/O
String I/O
Array I/O
(deprecated in C++98)
(deprecated in C++98)
(deprecated in C++98)
Synchronized Output
Types
Error category interface
(C++11)
 
 
(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 );
(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 );
template< class CharT, class Traits, class T >

basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os,

                                            const T& value );
(3) (since C++11)

Inserts a character or a character string.

1) Behaves as an FormattedOutputFunction. After constructing and checking the sentry object, inserts the character 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.
2) Behaves as an FormattedOutputFunction. After constructing and checking the sentry object, inserts successive characters from the character array whose first element is pointed to by s.
  • for the first and third overloads (where CharT matches the type of ch), exactly traits::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.
3) Calls the appropriate insertion operator, given an rvalue reference to an output stream object (equivalent to os << value). This function template does not participate in overload resolution unless the expression os << value is well-formed. (since C++17)

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

Notes

Overload (3) in LLVM libc++ implements LWG#1203 and returns a stream of the same type as the argument, so that code such as (std::ostringstream() << 1.2).str() compiles.

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

See also

inserts formatted data
(public member function)
widens characters
(public member function of std::basic_ios<CharT,Traits>)