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