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