CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Jul 2025 05:54:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/ostream/basic_ostream/operator-free/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 24 Jul 2025 05:54:58 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"8fd6-QvzAY7kzWbDBg41EHuORxxTf3/4"
Content-Encoding: gzip
This operator (
The function inserts at least
Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it inserts the characters described above into its associated stream buffer object as if calling its member function sputc. It then resets the field width to zero, and finally destroys the sentry object before returning.
Errors are signaled by modifying the internal state flags:
Multiple flags may be set by a single operation.
If the operation sets an internal state flag on os that was registered with its member exceptions, the function throws an exception of its failure member type.
Output:
Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog, wcout, wcerr and wclog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
It throws an exception of member type failure if the resulting error state flag for is is not goodbit and member exceptions was set to throw for that state in os.
Any exception thrown by an internal operation is caught and handled by the function, setting is's badbit flag. If badbit was set on the last call to exceptions for is, the function rethrows the caught exception.
If s does not point to a null terminated character sequence, it causes undefined behavior.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
basic_ostream
- basic_ostream::~basic_ostream
- basic_ostream::basic_ostream
member classes
member functions
non-member overloads
protected members
-
basic_ostream::operator=C++11
-
basic_ostream::swapC++11
-
- Reference
- <ostream>
- basic_ostream
- operator<<
public member function
<ostream> <iostream>
std::operator<< (basic_ostream)
single character (1) | template<class charT, class traits>basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, charT c);template<class charT, class traits>basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, char c);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, char c);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, signed char c);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, unsigned char c); |
---|---|
character sequence (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<charT,traits>& os, const char* s);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<charT,traits>& os, const signed char* s);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<charT,traits>& os, const unsigned char* s); |
single character (1) | template<class charT, class traits>basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, charT c);template<class charT, class traits>basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, char c);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, char c);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, signed char c);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, unsigned char c); |
---|---|
character sequence (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<charT,traits>& os, const char* s);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<charT,traits>& os, const signed char* s);template<class traits>basic_ostream<char,traits>& operator<< (basic_ostream<charT,traits>& os, const unsigned char* s); |
rvalue insertion (3) | template<class charT, class traits, class T>basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>&& os, const T& val); |
Insert characters
<<
) applied to an output stream is known as insertion operator, and performs formatted output:- (1) single character
- Inserts the character c into os.
- (2) character sequence
- Inserts the C-string s into os.
The terminating null character is not inserted into os.
The length of the c-string is determined beforehand by using the length member of the proper traits class.
- (3) rvalue insertion
- Allows inserting into rvalue basic_ostream objects, with the same effect as to lvalues: It effectively calls:
os<<val
.
The function inserts at least
os.width()
characters into os: if the characters to insert are less, they are padded with fill characters inserted before c or s, unless os's adjustfield setting is left (in this case, the fill characters are inserted after c or s).Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it inserts the characters described above into its associated stream buffer object as if calling its member function sputc. It then resets the field width to zero, and finally destroys the sentry object before returning.
Parameters
- os
- Output stream object where characters are inserted.
- c
- Character to insert.
If this is of typechar
, and this type is different from charT (the character type handled by os), the character inserted isos.widen(c)
.
- s
- Pointer to a null-terminated sequence of characters.
If these are of typechar
, and this type is different from charT (the character type handled by os), each character inserted is widened usingos.widen
.
- val
- ValueObject inserted.
T shall be a type supported as right-hand side argument either by this function or by os's member function operator<<.
Return Value
The basic_ostream object (os).Errors are signaled by modifying the internal state flags:
flag | error |
---|---|
eofbit | - |
failbit | The function failed while formatting the output (it may also be set if the construction of sentry failed). |
badbit | Either the insertion on the stream failed, or some other error happened (such as when this function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. |
If the operation sets an internal state flag on os that was registered with its member exceptions, the function throws an exception of its failure member type.
Example
|
|
Output:
Example string ****X |
Data races
Accesses s (2). Modifies os.Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog, wcout, wcerr and wclog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
Exception safety
Basic guarantee: if an exception is thrown, all objects involved are in valid states.It throws an exception of member type failure if the resulting error state flag for is is not goodbit and member exceptions was set to throw for that state in os.
Any exception thrown by an internal operation is caught and handled by the function, setting is's badbit flag. If badbit was set on the last call to exceptions for is, the function rethrows the caught exception.
If s does not point to a null terminated character sequence, it causes undefined behavior.
See also
- basic_ostream::operator<<
- Insert formatted output (public member function)
- basic_ostream::put
- Put character (public member function)
- operator>> (basic_istream)
- Extract characters (public member function)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us