libstdc++: ostream.h Source File

libstdc++

ostream.h
Go to the documentation of this file.
1// Output streams -*- C++ -*-
2
3// Copyright (C) 1997-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/ostream.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ostream}
28 */
29
30//
31// ISO C++ 14882: 27.6.2 Output streams
32//
33
34#ifndef _GLIBCXX_OSTREAM_H
35#define _GLIBCXX_OSTREAM_H 1
36
37#ifdef _GLIBCXX_SYSHDR
38#pragma GCC system_header
39#endif
40
41#include <bits/requires_hosted.h> // iostreams
42
43#include <ios>
44#include <bits/ostream_insert.h>
45
46# define __glibcxx_want_print
47#include <bits/version.h> // __glibcxx_syncbuf
48
49namespace std _GLIBCXX_VISIBILITY(default)
50{
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
52
53 /**
54 * @brief Template class basic_ostream.
55 * @ingroup io
56 *
57 * @tparam _CharT Type of character stream.
58 * @tparam _Traits Traits for character type, defaults to
59 * char_traits<_CharT>.
60 *
61 * This is the base class for all output streams. It provides text
62 * formatting of all builtin types, and communicates with any class
63 * derived from basic_streambuf to do the actual output.
64 */
65 template<typename _CharT, typename _Traits>
66 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
67 {
68 public:
69 // Types (inherited from basic_ios):
70 typedef _CharT char_type;
71 typedef typename _Traits::int_type int_type;
72 typedef typename _Traits::pos_type pos_type;
73 typedef typename _Traits::off_type off_type;
74 typedef _Traits traits_type;
75
76 // Non-standard Types:
77 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
78 typedef basic_ios<_CharT, _Traits> __ios_type;
79 typedef basic_ostream<_CharT, _Traits> __ostream_type;
80 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
81 __num_put_type;
82 typedef ctype<_CharT> __ctype_type;
83
84 /**
85 * @brief Base constructor.
86 *
87 * This ctor is almost never called by the user directly, rather from
88 * derived classes' initialization lists, which pass a pointer to
89 * their own stream buffer.
90 */
91 explicit
92 basic_ostream(__streambuf_type* __sb)
93 { this->init(__sb); }
94
95 /**
96 * @brief Base destructor.
97 *
98 * This does very little apart from providing a virtual base dtor.
99 */
100 virtual
101 ~basic_ostream() { }
102
103 /// Safe prefix/suffix operations.
104 class sentry;
105 friend class sentry;
106
107 ///@{
108 /**
109 * @brief Interface for manipulators.
110 *
111 * Manipulators such as @c std::endl and @c std::hex use these
112 * functions in constructs like "std::cout << std::endl". For more
113 * information, see the iomanip header.
114 */
115 __ostream_type&
116 operator<<(__ostream_type& (*__pf)(__ostream_type&))
117 {
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // DR 60. What is a formatted input function?
120 // The inserters for manipulators are *not* formatted output functions.
121 return __pf(*this);
122 }
123
124 __ostream_type&
125 operator<<(__ios_type& (*__pf)(__ios_type&))
126 {
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // DR 60. What is a formatted input function?
129 // The inserters for manipulators are *not* formatted output functions.
130 __pf(*this);
131 return *this;
132 }
133
134 __ostream_type&
135 operator<<(ios_base& (*__pf) (ios_base&))
136 {
137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 // DR 60. What is a formatted input function?
139 // The inserters for manipulators are *not* formatted output functions.
140 __pf(*this);
141 return *this;
142 }
143 ///@}
144
145 ///@{
146 /**
147 * @name Inserters
148 *
149 * All the @c operator<< functions (aka <em>formatted output
150 * functions</em>) have some common behavior. Each starts by
151 * constructing a temporary object of type std::basic_ostream::sentry.
152 * This can have several effects, concluding with the setting of a
153 * status flag; see the sentry documentation for more.
154 *
155 * If the sentry status is good, the function tries to generate
156 * whatever data is appropriate for the type of the argument.
157 *
158 * If an exception is thrown during insertion, ios_base::badbit
159 * will be turned on in the stream's error state without causing an
160 * ios_base::failure to be thrown. The original exception will then
161 * be rethrown.
162 */
163
164 ///@{
165 /**
166 * @brief Integer arithmetic inserters
167 * @param __n A variable of builtin integral type.
168 * @return @c *this if successful
169 *
170 * These functions use the stream's current locale (specifically, the
171 * @c num_get facet) to perform numeric formatting.
172 */
173 __ostream_type&
174 operator<<(long __n)
175 { return _M_insert(__n); }
176
177 __ostream_type&
178 operator<<(unsigned long __n)
179 { return _M_insert(__n); }
180
181 __ostream_type&
182 operator<<(bool __n)
183 { return _M_insert(__n); }
184
185 __ostream_type&
186 operator<<(short __n);
187
188 __ostream_type&
189 operator<<(unsigned short __n)
190 {
191 // _GLIBCXX_RESOLVE_LIB_DEFECTS
192 // 117. basic_ostream uses nonexistent num_put member functions.
193 return _M_insert(static_cast<unsigned long>(__n));
194 }
195
196 __ostream_type&
197 operator<<(int __n);
198
199 __ostream_type&
200 operator<<(unsigned int __n)
201 {
202 // _GLIBCXX_RESOLVE_LIB_DEFECTS
203 // 117. basic_ostream uses nonexistent num_put member functions.
204 return _M_insert(static_cast<unsigned long>(__n));
205 }
206
207#ifdef _GLIBCXX_USE_LONG_LONG
208#pragma GCC diagnostic push
209#pragma GCC diagnostic ignored "-Wlong-long"
210 __ostream_type&
211 operator<<(long long __n)
212 { return _M_insert(__n); }
213
214 __ostream_type&
215 operator<<(unsigned long long __n)
216 { return _M_insert(__n); }
217#pragma GCC diagnostic pop
218#endif
219 ///@}
220
221 ///@{
222 /**
223 * @brief Floating point arithmetic inserters
224 * @param __f A variable of builtin floating point type.
225 * @return @c *this if successful
226 *
227 * These functions use the stream's current locale (specifically, the
228 * @c num_get facet) to perform numeric formatting.
229 */
230 __ostream_type&
231 operator<<(double __f)
232 { return _M_insert(__f); }
233
234 __ostream_type&
235 operator<<(float __f)
236 {
237 // _GLIBCXX_RESOLVE_LIB_DEFECTS
238 // 117. basic_ostream uses nonexistent num_put member functions.
239 return _M_insert(_S_cast_flt<double>(__f));
240 }
241
242 __ostream_type&
243 operator<<(long double __f)
244 { return _M_insert(__f); }
245 ///@}
246
247#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
248 __attribute__((__always_inline__))
249 __ostream_type&
250 operator<<(_Float16 __f)
251 {
252 return _M_insert(_S_cast_flt<double>(__f));
253 }
254#endif
255
256#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
257 __attribute__((__always_inline__))
258 __ostream_type&
259 operator<<(_Float32 __f)
260 {
261 return _M_insert(_S_cast_flt<double>(__f));
262 }
263#endif
264
265#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
266 __attribute__((__always_inline__))
267 __ostream_type&
268 operator<<(_Float64 __f)
269 {
270 return _M_insert(_S_cast_flt<double>(__f));
271 }
272#endif
273
274#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
275 __attribute__((__always_inline__))
276 __ostream_type&
277 operator<<(_Float128 __f)
278 {
279 return _M_insert(_S_cast_flt<long double>(__f));
280 }
281#endif
282
283#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
284 __attribute__((__always_inline__))
285 __ostream_type&
286 operator<<(__gnu_cxx::__bfloat16_t __f)
287 {
288 return _M_insert(_S_cast_flt<double>(__f));
289 }
290#endif
291
292 /**
293 * @brief Pointer arithmetic inserters
294 * @param __p A variable of pointer type.
295 * @return @c *this if successful
296 *
297 * These functions use the stream's current locale (specifically, the
298 * @c num_get facet) to perform numeric formatting.
299 */
300 __ostream_type&
301 operator<<(const void* __p)
302 { return _M_insert(__p); }
303
304#if __cplusplus >= 201703L
305 __ostream_type&
306 operator<<(nullptr_t)
307 { return *this << "nullptr"; }
308#endif
309
310#if __cplusplus > 202002L
311 __attribute__((__always_inline__))
312 __ostream_type&
313 operator<<(const volatile void* __p)
314 { return _M_insert(const_cast<const void*>(__p)); }
315#endif
316
317 /**
318 * @brief Extracting from another streambuf.
319 * @param __sb A pointer to a streambuf
320 *
321 * This function behaves like one of the basic arithmetic extractors,
322 * in that it also constructs a sentry object and has the same error
323 * handling behavior.
324 *
325 * If @p __sb is NULL, the stream will set failbit in its error state.
326 *
327 * Characters are extracted from @p __sb and inserted into @c *this
328 * until one of the following occurs:
329 *
330 * - the input stream reaches end-of-file,
331 * - insertion into the output sequence fails (in this case, the
332 * character that would have been inserted is not extracted), or
333 * - an exception occurs while getting a character from @p __sb, which
334 * sets failbit in the error state
335 *
336 * If the function inserts no characters, failbit is set.
337 */
338 __ostream_type&
339 operator<<(__streambuf_type* __sb);
340 ///@}
341
342 ///@{
343 /**
344 * @name Unformatted Output Functions
345 *
346 * All the unformatted output functions have some common behavior.
347 * Each starts by constructing a temporary object of type
348 * std::basic_ostream::sentry. This has several effects, concluding
349 * with the setting of a status flag; see the sentry documentation
350 * for more.
351 *
352 * If the sentry status is good, the function tries to generate
353 * whatever data is appropriate for the type of the argument.
354 *
355 * If an exception is thrown during insertion, ios_base::badbit
356 * will be turned on in the stream's error state. If badbit is on in
357 * the stream's exceptions mask, the exception will be rethrown
358 * without completing its actions.
359 */
360
361 /**
362 * @brief Simple insertion.
363 * @param __c The character to insert.
364 * @return *this
365 *
366 * Tries to insert @p __c.
367 *
368 * @note This function is not overloaded on signed char and
369 * unsigned char.
370 */
371 __ostream_type&
372 put(char_type __c);
373
374 /**
375 * @brief Character string insertion.
376 * @param __s The array to insert.
377 * @param __n Maximum number of characters to insert.
378 * @return *this
379 *
380 * Characters are copied from @p __s and inserted into the stream until
381 * one of the following happens:
382 *
383 * - @p __n characters are inserted
384 * - inserting into the output sequence fails (in this case, badbit
385 * will be set in the stream's error state)
386 *
387 * @note This function is not overloaded on signed char and
388 * unsigned char.
389 */
390 __ostream_type&
391 write(const char_type* __s, streamsize __n);
392 ///@}
393
394 /**
395 * @brief Synchronizing the stream buffer.
396 * @return *this
397 *
398 * If @c rdbuf() is a null pointer, changes nothing.
399 *
400 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
401 * sets badbit.
402 */
403 __ostream_type&
404 flush();
405
406 /**
407 * @brief Getting the current write position.
408 * @return A file position object.
409 *
410 * If @c fail() is not false, returns @c pos_type(-1) to indicate
411 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
412 */
413 pos_type
414 tellp();
415
416 /**
417 * @brief Changing the current write position.
418 * @param __pos A file position object.
419 * @return *this
420 *
421 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
422 * that function fails, sets failbit.
423 */
424 __ostream_type&
425 seekp(pos_type);
426
427 /**
428 * @brief Changing the current write position.
429 * @param __off A file offset object.
430 * @param __dir The direction in which to seek.
431 * @return *this
432 *
433 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
434 * If that function fails, sets failbit.
435 */
436 __ostream_type&
437 seekp(off_type, ios_base::seekdir);
438
439 protected:
440 basic_ostream()
441 { this->init(0); }
442
443#if __cplusplus >= 201103L
444 // Non-standard constructor that does not call init()
445 basic_ostream(basic_iostream<_CharT, _Traits>&) { }
446
447 basic_ostream(const basic_ostream&) = delete;
448
449 basic_ostream(basic_ostream&& __rhs)
450 : __ios_type()
451 { __ios_type::move(__rhs); }
452
453 // 27.7.3.3 Assign/swap
454
455 basic_ostream& operator=(const basic_ostream&) = delete;
456
457 basic_ostream&
458 operator=(basic_ostream&& __rhs)
459 {
460 swap(__rhs);
461 return *this;
462 }
463
464 void
465 swap(basic_ostream& __rhs)
466 { __ios_type::swap(__rhs); }
467#endif
468
469 template<typename _ValueT>
470 __ostream_type&
471 _M_insert(_ValueT __v);
472
473 private:
474#if !_GLIBCXX_INLINE_VERSION
475 void
476 _M_write(const char_type* __s, streamsize __n)
477 { std::__ostream_insert(*this, __s, __n); }
478#endif
479
480#pragma GCC diagnostic push
481#pragma GCC diagnostic ignored "-Wc++17-extensions" // for if-constexpr
482 template<typename _To, typename _From>
483 static _To
484 _S_cast_flt(_From __f)
485 {
486 _To __d = static_cast<_To>(__f);
487 // _GLIBCXX_RESOLVE_LIB_DEFECTS
488 // 4101: LWG 117 loses the sign for negative NaN on some arches.
489#if defined __riscv
490 _To __sign;
491#if __cpp_constexpr && __has_builtin(__builtin_bit_cast)
492 if constexpr (sizeof(__f) == sizeof(short))
493 __sign = static_cast<_To>(__builtin_bit_cast(short, __f));
494 else if constexpr (sizeof(__f) == sizeof(int))
495 __sign = static_cast<_To>(__builtin_bit_cast(int, __f));
496 else if constexpr (sizeof(__f) == sizeof(long long))
497 __sign = static_cast<_To>(__builtin_bit_cast(long long, __f));
498 else
499#endif
500 __sign = __builtin_signbit(__f) ? _To(-1.0) : _To(+1.0);
501
502 if _GLIBCXX_CONSTEXPR (__is_same(_To, double))
503 __d = __builtin_copysign(__d, __sign);
504 else if _GLIBCXX_CONSTEXPR (__is_same(_To, long double))
505 __d = __builtin_copysignl(__d, __sign);
506#endif
507 return __d;
508 }
509#pragma GCC diagnostic pop
510
511 // RAII type to clear and restore an ostream's exceptions mask.
512 struct _Disable_exceptions
513 {
514 _Disable_exceptions(basic_ostream& __os)
515 : _M_os(__os), _M_exception(_M_os._M_exception)
516 { _M_os._M_exception = ios_base::goodbit; }
517
518 ~_Disable_exceptions()
519 { _M_os._M_exception = _M_exception; }
520
521#pragma GCC diagnostic push
522#pragma GCC diagnostic ignored "-Wc++11-extensions" // deleted functions
523 _Disable_exceptions(const _Disable_exceptions&) = delete;
524 _Disable_exceptions& operator=(const _Disable_exceptions&) = delete;
525#pragma GCC diagnostic pop
526
527 private:
528 basic_ostream& _M_os;
529 const ios_base::iostate _M_exception;
530 };
531 };
532
533 /**
534 * @brief Performs setup work for output streams.
535 *
536 * Objects of this class are created before all of the standard
537 * inserters are run. It is responsible for <em>exception-safe prefix and
538 * suffix operations</em>.
539 */
540 template <typename _CharT, typename _Traits>
541 class basic_ostream<_CharT, _Traits>::sentry
542 {
543 // Data Members.
544 bool _M_ok;
545 basic_ostream<_CharT, _Traits>& _M_os;
546
547 public:
548 /**
549 * @brief The constructor performs preparatory work.
550 * @param __os The output stream to guard.
551 *
552 * If the stream state is good (@a __os.good() is true), then if the
553 * stream is tied to another output stream, @c is.tie()->flush()
554 * is called to synchronize the output sequences.
555 *
556 * If the stream state is still good, then the sentry state becomes
557 * true (@a okay).
558 */
559 explicit
560 sentry(basic_ostream<_CharT, _Traits>& __os);
561
562#pragma GCC diagnostic push
563#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
564 /**
565 * @brief Possibly flushes the stream.
566 *
567 * If `ios_base::unitbuf` is set in `os.flags()`, and
568 * `std::uncaught_exception()` is true, the sentry destructor flushes
569 * the output stream.
570 */
571 ~sentry()
572 {
573 // _GLIBCXX_RESOLVE_LIB_DEFECTS
574 // 397. ostream::sentry dtor throws exceptions
575 // 835. Tying two streams together (correction to DR 581)
576 // 4188. ostream::sentry destructor should handle exceptions
577 if (bool(_M_os.flags() & ios_base::unitbuf) && _M_os.good()
578 && !uncaught_exception()) // XXX MT
579 {
580 _Disable_exceptions __noex(_M_os);
581 __try
582 {
583 // Can't call _M_os.flush() directly because that constructs
584 // another sentry.
585 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
586 _M_os.setstate(ios_base::badbit);
587 }
588 __catch(...)
589 { _M_os.setstate(ios_base::badbit); }
590 }
591 }
592#pragma GCC diagnostic pop
593
594 /**
595 * @brief Quick status checking.
596 * @return The sentry state.
597 *
598 * For ease of use, sentries may be converted to booleans. The
599 * return value is that of the sentry state (true == okay).
600 */
601#if __cplusplus >= 201103L
602 explicit
603#endif
604 operator bool() const
605 { return _M_ok; }
606 };
607
608 ///@{
609 /**
610 * @brief Character inserters
611 * @param __out An output stream.
612 * @param __c A character.
613 * @return out
614 *
615 * Behaves like one of the formatted arithmetic inserters described in
616 * std::basic_ostream. After constructing a sentry object with good
617 * status, this function inserts a single character and any required
618 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
619 * called.
620 *
621 * If @p __c is of type @c char and the character type of the stream is not
622 * @c char, the character is widened before insertion.
623 */
624 template<typename _CharT, typename _Traits>
625 inline basic_ostream<_CharT, _Traits>&
626 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
627 {
628 if (__out.width() != 0)
629 return __ostream_insert(__out, &__c, 1);
630 __out.put(__c);
631 return __out;
632 }
633
634 template<typename _CharT, typename _Traits>
635 inline basic_ostream<_CharT, _Traits>&
636 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
637 { return (__out << __out.widen(__c)); }
638
639 // Specialization
640 template<typename _Traits>
641 inline basic_ostream<char, _Traits>&
642 operator<<(basic_ostream<char, _Traits>& __out, char __c)
643 {
644 if (__out.width() != 0)
645 return __ostream_insert(__out, &__c, 1);
646 __out.put(__c);
647 return __out;
648 }
649
650 // Signed and unsigned
651 template<typename _Traits>
652 inline basic_ostream<char, _Traits>&
653 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
654 { return (__out << static_cast<char>(__c)); }
655
656 template<typename _Traits>
657 inline basic_ostream<char, _Traits>&
658 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
659 { return (__out << static_cast<char>(__c)); }
660 ///@}
661
662#if __cplusplus > 201703L
663 // The following deleted overloads prevent formatting character values as
664 // numeric values.
665
666 template<typename _Traits>
667 basic_ostream<char, _Traits>&
668 operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
669
670#ifdef _GLIBCXX_USE_CHAR8_T
671 template<typename _Traits>
672 basic_ostream<char, _Traits>&
673 operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
674#endif
675
676 template<typename _Traits>
677 basic_ostream<char, _Traits>&
678 operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
679
680 template<typename _Traits>
681 basic_ostream<char, _Traits>&
682 operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
683
684#ifdef _GLIBCXX_USE_WCHAR_T
685#ifdef _GLIBCXX_USE_CHAR8_T
686 template<typename _Traits>
687 basic_ostream<wchar_t, _Traits>&
688 operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
689#endif // _GLIBCXX_USE_CHAR8_T
690
691 template<typename _Traits>
692 basic_ostream<wchar_t, _Traits>&
693 operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
694
695 template<typename _Traits>
696 basic_ostream<wchar_t, _Traits>&
697 operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
698#endif // _GLIBCXX_USE_WCHAR_T
699#endif // C++20
700
701 ///@{
702 /**
703 * @brief String inserters
704 * @param __out An output stream.
705 * @param __s A character string.
706 * @return out
707 * @pre @p __s must be a non-NULL pointer
708 *
709 * Behaves like one of the formatted arithmetic inserters described in
710 * std::basic_ostream. After constructing a sentry object with good
711 * status, this function inserts @c traits::length(__s) characters starting
712 * at @p __s, widened if necessary, followed by any required padding (as
713 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
714 */
715 template<typename _CharT, typename _Traits>
716 inline basic_ostream<_CharT, _Traits>&
717 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
718 {
719 if (!__s)
720 __out.setstate(ios_base::badbit);
721 else
722 __ostream_insert(__out, __s,
723 static_cast<streamsize>(_Traits::length(__s)));
724 return __out;
725 }
726
727 template<typename _CharT, typename _Traits>
728 basic_ostream<_CharT, _Traits> &
729 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
730
731 // Partial specializations
732 template<typename _Traits>
733 inline basic_ostream<char, _Traits>&
734 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
735 {
736 if (!__s)
737 __out.setstate(ios_base::badbit);
738 else
739 __ostream_insert(__out, __s,
740 static_cast<streamsize>(_Traits::length(__s)));
741 return __out;
742 }
743
744 // Signed and unsigned
745 template<typename _Traits>
746 inline basic_ostream<char, _Traits>&
747 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
748 { return (__out << reinterpret_cast<const char*>(__s)); }
749
750 template<typename _Traits>
751 inline basic_ostream<char, _Traits> &
752 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
753 { return (__out << reinterpret_cast<const char*>(__s)); }
754 ///@}
755
756#if __cplusplus > 201703L
757 // The following deleted overloads prevent formatting strings as
758 // pointer values.
759
760 template<typename _Traits>
761 basic_ostream<char, _Traits>&
762 operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
763
764#ifdef _GLIBCXX_USE_CHAR8_T
765 template<typename _Traits>
766 basic_ostream<char, _Traits>&
767 operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
768#endif // _GLIBCXX_USE_CHAR8_T
769
770 template<typename _Traits>
771 basic_ostream<char, _Traits>&
772 operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
773
774 template<typename _Traits>
775 basic_ostream<char, _Traits>&
776 operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
777
778#ifdef _GLIBCXX_USE_WCHAR_T
779#ifdef _GLIBCXX_USE_CHAR8_T
780 template<typename _Traits>
781 basic_ostream<wchar_t, _Traits>&
782 operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
783#endif
784
785 template<typename _Traits>
786 basic_ostream<wchar_t, _Traits>&
787 operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
788
789 template<typename _Traits>
790 basic_ostream<wchar_t, _Traits>&
791 operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
792#endif // _GLIBCXX_USE_WCHAR_T
793#endif // C++20
794
795#if __cplusplus >= 201103L
796 // C++11 27.7.3.9 Rvalue stream insertion [ostream.rvalue]
797 // _GLIBCXX_RESOLVE_LIB_DEFECTS
798 // 1203. More useful rvalue stream insertion
799
800#if __cpp_concepts >= 201907L && __glibcxx_type_trait_variable_templates
801 // Use concepts if possible because they're cheaper to evaluate.
802 template<typename _Tp>
803 concept __derived_from_ios_base = is_class_v<_Tp>
804 && (!is_same_v<_Tp, ios_base>)
805 && requires (_Tp* __t, ios_base* __b) { __b = __t; };
806
807 template<typename _Os, typename _Tp>
808 requires __derived_from_ios_base<_Os>
809 && requires (_Os& __os, const _Tp& __t) { __os << __t; }
810 using __rvalue_stream_insertion_t = _Os&&;
811#else
812 template<typename _Tp>
813 using _Require_derived_from_ios_base
814 = _Require<is_class<_Tp>, __not_<is_same<_Tp, ios_base>>,
815 is_convertible<typename add_pointer<_Tp>::type, ios_base*>>;
816
817 template<typename _Os, typename _Tp,
818 typename = _Require_derived_from_ios_base<_Os>,
819 typename
820 = decltype(std::declval<_Os&>() << std::declval<const _Tp&>())>
821 using __rvalue_stream_insertion_t = _Os&&;
822#endif
823
824 /**
825 * @brief Generic inserter for rvalue stream
826 * @param __os An input stream.
827 * @param __x A reference to the object being inserted.
828 * @return __os
829 *
830 * This is just a forwarding function to allow insertion to
831 * rvalue streams since they won't bind to the inserter functions
832 * that take an lvalue reference.
833 */
834 template<typename _Ostream, typename _Tp>
835 inline __rvalue_stream_insertion_t<_Ostream, _Tp>
836 operator<<(_Ostream&& __os, const _Tp& __x)
837 {
838 __os << __x;
839 return std::move(__os);
840 }
841#endif // C++11
842
843_GLIBCXX_END_NAMESPACE_VERSION
844} // namespace std
845
846#endif /* _GLIBCXX_OSTREAM_H */
ios
version.h
ostream_insert.h
requires_hosted.h
std::declval
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2718
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::uncaught_exception
bool uncaught_exception() noexcept
std
ISO C++ entities toplevel namespace is std.
std::streamsize
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
std::operator<<
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1754
std::basic_ios::setstate
void setstate(iostate __state)
Sets additional flags in the error state.
Definition basic_ios.h:167
std::basic_ios::init
void init(basic_streambuf< _CharT, _Traits > *__sb)
All setup is performed here.
Definition basic_ios.tcc:131
std::basic_ios::basic_ios
basic_ios(basic_streambuf< _CharT, _Traits > *__sb)
Constructor performs initialization.
Definition basic_ios.h:285
std::basic_streambuf
The actual work of input and output (interface).
Definition streambuf:127
std::basic_ostream
Template class basic_ostream.
Definition ostream.h:67
std::basic_ostream::write
__ostream_type & write(const char_type *__s, streamsize __n)
Character string insertion.
Definition ostream.tcc:192
std::basic_ostream::tellp
pos_type tellp()
Getting the current write position.
Definition ostream.tcc:261
std::basic_ostream::seekp
__ostream_type & seekp(off_type, ios_base::seekdir)
Changing the current write position.
Definition ostream.tcc:292
std::basic_ostream::put
__ostream_type & put(char_type __c)
Simple insertion.
Definition ostream.tcc:158
std::basic_ostream::basic_ostream
basic_ostream(__streambuf_type *__sb)
Base constructor.
Definition ostream.h:92
std::basic_ostream::flush
__ostream_type & flush()
Synchronizing the stream buffer.
Definition ostream.tcc:226
std::basic_ostream::seekp
__ostream_type & seekp(pos_type)
Changing the current write position.
Definition ostream.tcc:273
std::basic_ostream::~basic_ostream
virtual ~basic_ostream()
Base destructor.
Definition ostream.h:101
std::basic_ostream::operator<<
__ostream_type & operator<<(__ostream_type &(*__pf)(__ostream_type &))
Interface for manipulators.
Definition ostream.h:116
std::basic_iostream
Template class basic_iostream.
Definition istream:999
std::ios_base
The base of the I/O class hierarchy.
Definition ios_base.h:266
std::ios_base::iostate
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:453
std::ios_base::goodbit
static const iostate goodbit
Indicates all is well.
Definition ios_base.h:468
std::ios_base::unitbuf
static const fmtflags unitbuf
Flushes output after each output operation.
Definition ios_base.h:426
std::ios_base::badbit
static const iostate badbit
Indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition ios_base.h:457
std::ios_base::width
streamsize width() const
Flags access.
Definition ios_base.h:789
std::ios_base::seekdir
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:523
std::ctype
Primary class template ctype facet.
Definition locale_facets.h:618
std::num_put
Primary class template num_put.
Definition locale_facets.h:2327
std::basic_ostream::sentry::~sentry
~sentry()
Possibly flushes the stream.
Definition ostream.h:571
std::basic_ostream::sentry::sentry
sentry(basic_ostream< _CharT, _Traits > &__os)
The constructor performs preparatory work.
Definition ostream.tcc:51