1// Output streams -*- C++ -*-
3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25/** @file include/ostream
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 27.6.2 Output streams
33#ifndef _GLIBCXX_OSTREAM
34#define _GLIBCXX_OSTREAM 1
36#pragma GCC system_header
38#include <bits/requires_hosted.h> // iostreams
41#include <bits/ostream_insert.h>
43namespace std _GLIBCXX_VISIBILITY(default)
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @brief Template class basic_ostream.
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
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.
59 template<typename _CharT, typename _Traits>
60 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
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;
70 // Non-standard Types:
71 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
72 typedef basic_ios<_CharT, _Traits> __ios_type;
73 typedef basic_ostream<_CharT, _Traits> __ostream_type;
74 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
76 typedef ctype<_CharT> __ctype_type;
79 * @brief Base constructor.
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.
86 basic_ostream(__streambuf_type* __sb)
90 * @brief Base destructor.
92 * This does very little apart from providing a virtual base dtor.
97 /// Safe prefix/suffix operations.
103 * @brief Interface for manipulators.
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.
110 operator<<(__ostream_type& (*__pf)(__ostream_type&))
112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
113 // DR 60. What is a formatted input function?
114 // The inserters for manipulators are *not* formatted output functions.
119 operator<<(__ios_type& (*__pf)(__ios_type&))
121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
122 // DR 60. What is a formatted input function?
123 // The inserters for manipulators are *not* formatted output functions.
129 operator<<(ios_base& (*__pf) (ios_base&))
131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
132 // DR 60. What is a formatted input function?
133 // The inserters for manipulators are *not* formatted output functions.
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.
149 * If the sentry status is good, the function tries to generate
150 * whatever data is appropriate for the type of the argument.
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
160 * @brief Integer arithmetic inserters
161 * @param __n A variable of builtin integral type.
162 * @return @c *this if successful
164 * These functions use the stream's current locale (specifically, the
165 * @c num_get facet) to perform numeric formatting.
169 { return _M_insert(__n); }
172 operator<<(unsigned long __n)
173 { return _M_insert(__n); }
177 { return _M_insert(__n); }
180 operator<<(short __n);
183 operator<<(unsigned short __n)
185 // _GLIBCXX_RESOLVE_LIB_DEFECTS
186 // 117. basic_ostream uses nonexistent num_put member functions.
187 return _M_insert(static_cast<unsigned long>(__n));
194 operator<<(unsigned int __n)
196 // _GLIBCXX_RESOLVE_LIB_DEFECTS
197 // 117. basic_ostream uses nonexistent num_put member functions.
198 return _M_insert(static_cast<unsigned long>(__n));
201#ifdef _GLIBCXX_USE_LONG_LONG
203 operator<<(long long __n)
204 { return _M_insert(__n); }
207 operator<<(unsigned long long __n)
208 { return _M_insert(__n); }
214 * @brief Floating point arithmetic inserters
215 * @param __f A variable of builtin floating point type.
216 * @return @c *this if successful
218 * These functions use the stream's current locale (specifically, the
219 * @c num_get facet) to perform numeric formatting.
222 operator<<(double __f)
223 { return _M_insert(__f); }
226 operator<<(float __f)
228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
229 // 117. basic_ostream uses nonexistent num_put member functions.
230 return _M_insert(static_cast<double>(__f));
234 operator<<(long double __f)
235 { return _M_insert(__f); }
238#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
239 __attribute__((__always_inline__))
241 operator<<(_Float16 __f)
243 return _M_insert(static_cast<double>(__f));
247#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
248 __attribute__((__always_inline__))
250 operator<<(_Float32 __f)
252 return _M_insert(static_cast<double>(__f));
256#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
257 __attribute__((__always_inline__))
259 operator<<(_Float64 __f)
261 return _M_insert(static_cast<double>(__f));
265#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
266 __attribute__((__always_inline__))
268 operator<<(_Float128 __f)
270 return _M_insert(static_cast<long double>(__f));
274#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
275 __attribute__((__always_inline__))
277 operator<<(__gnu_cxx::__bfloat16_t __f)
279 return _M_insert(static_cast<double>(__f));
284 * @brief Pointer arithmetic inserters
285 * @param __p A variable of pointer type.
286 * @return @c *this if successful
288 * These functions use the stream's current locale (specifically, the
289 * @c num_get facet) to perform numeric formatting.
292 operator<<(const void* __p)
293 { return _M_insert(__p); }
295#if __cplusplus >= 201703L
297 operator<<(nullptr_t)
298 { return *this << "nullptr"; }
301#if __cplusplus > 202002L
302 __attribute__((__always_inline__))
304 operator<<(const volatile void* __p)
305 { return _M_insert(const_cast<const void*>(__p)); }
309 * @brief Extracting from another streambuf.
310 * @param __sb A pointer to a streambuf
312 * This function behaves like one of the basic arithmetic extractors,
313 * in that it also constructs a sentry object and has the same error
316 * If @p __sb is NULL, the stream will set failbit in its error state.
318 * Characters are extracted from @p __sb and inserted into @c *this
319 * until one of the following occurs:
321 * - the input stream reaches end-of-file,
322 * - insertion into the output sequence fails (in this case, the
323 * character that would have been inserted is not extracted), or
324 * - an exception occurs while getting a character from @p __sb, which
325 * sets failbit in the error state
327 * If the function inserts no characters, failbit is set.
330 operator<<(__streambuf_type* __sb);
335 * @name Unformatted Output Functions
337 * All the unformatted output functions have some common behavior.
338 * Each starts by constructing a temporary object of type
339 * std::basic_ostream::sentry. This has several effects, concluding
340 * with the setting of a status flag; see the sentry documentation
343 * If the sentry status is good, the function tries to generate
344 * whatever data is appropriate for the type of the argument.
346 * If an exception is thrown during insertion, ios_base::badbit
347 * will be turned on in the stream's error state. If badbit is on in
348 * the stream's exceptions mask, the exception will be rethrown
349 * without completing its actions.
353 * @brief Simple insertion.
354 * @param __c The character to insert.
357 * Tries to insert @p __c.
359 * @note This function is not overloaded on signed char and
366 * @brief Character string insertion.
367 * @param __s The array to insert.
368 * @param __n Maximum number of characters to insert.
371 * Characters are copied from @p __s and inserted into the stream until
372 * one of the following happens:
374 * - @p __n characters are inserted
375 * - inserting into the output sequence fails (in this case, badbit
376 * will be set in the stream's error state)
378 * @note This function is not overloaded on signed char and
382 write(const char_type* __s, streamsize __n);
386 * @brief Synchronizing the stream buffer.
389 * If @c rdbuf() is a null pointer, changes nothing.
391 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
398 * @brief Getting the current write position.
399 * @return A file position object.
401 * If @c fail() is not false, returns @c pos_type(-1) to indicate
402 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
408 * @brief Changing the current write position.
409 * @param __pos A file position object.
412 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
413 * that function fails, sets failbit.
419 * @brief Changing the current write position.
420 * @param __off A file offset object.
421 * @param __dir The direction in which to seek.
424 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
425 * If that function fails, sets failbit.
428 seekp(off_type, ios_base::seekdir);
434#if __cplusplus >= 201103L
435 // Non-standard constructor that does not call init()
436 basic_ostream(basic_iostream<_CharT, _Traits>&) { }
438 basic_ostream(const basic_ostream&) = delete;
440 basic_ostream(basic_ostream&& __rhs)
442 { __ios_type::move(__rhs); }
444 // 27.7.3.3 Assign/swap
446 basic_ostream& operator=(const basic_ostream&) = delete;
449 operator=(basic_ostream&& __rhs)
456 swap(basic_ostream& __rhs)
457 { __ios_type::swap(__rhs); }
460 template<typename _ValueT>
462 _M_insert(_ValueT __v);
465#if !_GLIBCXX_INLINE_VERSION
467 _M_write(const char_type* __s, streamsize __n)
468 { std::__ostream_insert(*this, __s, __n); }
473 * @brief Performs setup work for output streams.
475 * Objects of this class are created before all of the standard
476 * inserters are run. It is responsible for <em>exception-safe prefix and
477 * suffix operations</em>.
479 template <typename _CharT, typename _Traits>
480 class basic_ostream<_CharT, _Traits>::sentry
484 basic_ostream<_CharT, _Traits>& _M_os;
488 * @brief The constructor performs preparatory work.
489 * @param __os The output stream to guard.
491 * If the stream state is good (@a __os.good() is true), then if the
492 * stream is tied to another output stream, @c is.tie()->flush()
493 * is called to synchronize the output sequences.
495 * If the stream state is still good, then the sentry state becomes
499 sentry(basic_ostream<_CharT, _Traits>& __os);
501#pragma GCC diagnostic push
502#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
504 * @brief Possibly flushes the stream.
506 * If @c ios_base::unitbuf is set in @c os.flags(), and
507 * @c std::uncaught_exception() is true, the sentry destructor calls
508 * @c flush() on the output stream.
513 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
515 // Can't call flush directly or else will get into recursive lock.
516 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
517 _M_os.setstate(ios_base::badbit);
520#pragma GCC diagnostic pop
523 * @brief Quick status checking.
524 * @return The sentry state.
526 * For ease of use, sentries may be converted to booleans. The
527 * return value is that of the sentry state (true == okay).
529#if __cplusplus >= 201103L
532 operator bool() const
538 * @brief Character inserters
539 * @param __out An output stream.
540 * @param __c A character.
543 * Behaves like one of the formatted arithmetic inserters described in
544 * std::basic_ostream. After constructing a sentry object with good
545 * status, this function inserts a single character and any required
546 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
549 * If @p __c is of type @c char and the character type of the stream is not
550 * @c char, the character is widened before insertion.
552 template<typename _CharT, typename _Traits>
553 inline basic_ostream<_CharT, _Traits>&
554 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
556 if (__out.width() != 0)
557 return __ostream_insert(__out, &__c, 1);
562 template<typename _CharT, typename _Traits>
563 inline basic_ostream<_CharT, _Traits>&
564 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
565 { return (__out << __out.widen(__c)); }
568 template<typename _Traits>
569 inline basic_ostream<char, _Traits>&
570 operator<<(basic_ostream<char, _Traits>& __out, char __c)
572 if (__out.width() != 0)
573 return __ostream_insert(__out, &__c, 1);
578 // Signed and unsigned
579 template<typename _Traits>
580 inline basic_ostream<char, _Traits>&
581 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
582 { return (__out << static_cast<char>(__c)); }
584 template<typename _Traits>
585 inline basic_ostream<char, _Traits>&
586 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
587 { return (__out << static_cast<char>(__c)); }
589#if __cplusplus > 201703L
590 // The following deleted overloads prevent formatting character values as
593 template<typename _Traits>
594 basic_ostream<char, _Traits>&
595 operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
597#ifdef _GLIBCXX_USE_CHAR8_T
598 template<typename _Traits>
599 basic_ostream<char, _Traits>&
600 operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
603 template<typename _Traits>
604 basic_ostream<char, _Traits>&
605 operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
607 template<typename _Traits>
608 basic_ostream<char, _Traits>&
609 operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
611#ifdef _GLIBCXX_USE_WCHAR_T
612#ifdef _GLIBCXX_USE_CHAR8_T
613 template<typename _Traits>
614 basic_ostream<wchar_t, _Traits>&
615 operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
616#endif // _GLIBCXX_USE_CHAR8_T
618 template<typename _Traits>
619 basic_ostream<wchar_t, _Traits>&
620 operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
622 template<typename _Traits>
623 basic_ostream<wchar_t, _Traits>&
624 operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
625#endif // _GLIBCXX_USE_WCHAR_T
631 * @brief String inserters
632 * @param __out An output stream.
633 * @param __s A character string.
635 * @pre @p __s must be a non-NULL pointer
637 * Behaves like one of the formatted arithmetic inserters described in
638 * std::basic_ostream. After constructing a sentry object with good
639 * status, this function inserts @c traits::length(__s) characters starting
640 * at @p __s, widened if necessary, followed by any required padding (as
641 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
643 template<typename _CharT, typename _Traits>
644 inline basic_ostream<_CharT, _Traits>&
645 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
648 __out.setstate(ios_base::badbit);
650 __ostream_insert(__out, __s,
651 static_cast<streamsize>(_Traits::length(__s)));
655 template<typename _CharT, typename _Traits>
656 basic_ostream<_CharT, _Traits> &
657 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
659 // Partial specializations
660 template<typename _Traits>
661 inline basic_ostream<char, _Traits>&
662 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
665 __out.setstate(ios_base::badbit);
667 __ostream_insert(__out, __s,
668 static_cast<streamsize>(_Traits::length(__s)));
672 // Signed and unsigned
673 template<typename _Traits>
674 inline basic_ostream<char, _Traits>&
675 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
676 { return (__out << reinterpret_cast<const char*>(__s)); }
678 template<typename _Traits>
679 inline basic_ostream<char, _Traits> &
680 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
681 { return (__out << reinterpret_cast<const char*>(__s)); }
683#if __cplusplus > 201703L
684 // The following deleted overloads prevent formatting strings as
687 template<typename _Traits>
688 basic_ostream<char, _Traits>&
689 operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
691#ifdef _GLIBCXX_USE_CHAR8_T
692 template<typename _Traits>
693 basic_ostream<char, _Traits>&
694 operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
695#endif // _GLIBCXX_USE_CHAR8_T
697 template<typename _Traits>
698 basic_ostream<char, _Traits>&
699 operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
701 template<typename _Traits>
702 basic_ostream<char, _Traits>&
703 operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
705#ifdef _GLIBCXX_USE_WCHAR_T
706#ifdef _GLIBCXX_USE_CHAR8_T
707 template<typename _Traits>
708 basic_ostream<wchar_t, _Traits>&
709 operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
712 template<typename _Traits>
713 basic_ostream<wchar_t, _Traits>&
714 operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
716 template<typename _Traits>
717 basic_ostream<wchar_t, _Traits>&
718 operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
719#endif // _GLIBCXX_USE_WCHAR_T
723 // Standard basic_ostream manipulators
726 * @brief Write a newline and flush the stream.
728 * This manipulator is often mistakenly used when a simple newline is
729 * desired, leading to poor buffering performance. See
730 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
731 * for more on this subject.
733 template<typename _CharT, typename _Traits>
734 inline basic_ostream<_CharT, _Traits>&
735 endl(basic_ostream<_CharT, _Traits>& __os)
736 { return flush(__os.put(__os.widen('\n'))); }
739 * @brief Write a null character into the output sequence.
741 * <em>Null character</em> is @c CharT() by definition. For CharT
742 * of @c char, this correctly writes the ASCII @c NUL character
745 template<typename _CharT, typename _Traits>
746 inline basic_ostream<_CharT, _Traits>&
747 ends(basic_ostream<_CharT, _Traits>& __os)
748 { return __os.put(_CharT()); }
751 * @brief Flushes the output stream.
753 * This manipulator simply calls the stream's @c flush() member function.
755 template<typename _CharT, typename _Traits>
756 inline basic_ostream<_CharT, _Traits>&
757 flush(basic_ostream<_CharT, _Traits>& __os)
758 { return __os.flush(); }
760#if __cplusplus >= 201103L
761 // C++11 27.7.3.9 Rvalue stream insertion [ostream.rvalue]
762 // _GLIBCXX_RESOLVE_LIB_DEFECTS
763 // 1203. More useful rvalue stream insertion
765#if __cpp_lib_concepts
766 // Use concepts if possible because they're cheaper to evaluate.
767 template<typename _Tp>
768 concept __derived_from_ios_base = is_class_v<_Tp>
769 && (!is_same_v<_Tp, ios_base>)
770 && requires (_Tp* __t, ios_base* __b) { __b = __t; };
772 template<typename _Os, typename _Tp>
773 requires __derived_from_ios_base<_Os>
774 && requires (_Os& __os, const _Tp& __t) { __os << __t; }
775 using __rvalue_stream_insertion_t = _Os&&;
777 template<typename _Tp>
778 using _Require_derived_from_ios_base
779 = _Require<is_class<_Tp>, __not_<is_same<_Tp, ios_base>>,
780 is_convertible<typename add_pointer<_Tp>::type, ios_base*>>;
782 template<typename _Os, typename _Tp,
783 typename = _Require_derived_from_ios_base<_Os>,
785 = decltype(std::declval<_Os&>() << std::declval<const _Tp&>())>
786 using __rvalue_stream_insertion_t = _Os&&;
790 * @brief Generic inserter for rvalue stream
791 * @param __os An input stream.
792 * @param __x A reference to the object being inserted.
795 * This is just a forwarding function to allow insertion to
796 * rvalue streams since they won't bind to the inserter functions
797 * that take an lvalue reference.
799 template<typename _Ostream, typename _Tp>
800 inline __rvalue_stream_insertion_t<_Ostream, _Tp>
801 operator<<(_Ostream&& __os, const _Tp& __x)
804 return std::move(__os);
807#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
808 template<typename _CharT, typename _Traits>
809 class __syncbuf_base : public basic_streambuf<_CharT, _Traits>
813 _S_get(basic_streambuf<_CharT, _Traits>* __buf [[maybe_unused]]) noexcept
816 if (auto __p = dynamic_cast<__syncbuf_base*>(__buf))
817 return &__p->_M_emit_on_sync;
823 __syncbuf_base(basic_streambuf<_CharT, _Traits>* __w = nullptr)
827 basic_streambuf<_CharT, _Traits>* _M_wrapped = nullptr;
828 bool _M_emit_on_sync = false;
829 bool _M_needs_sync = false;
832 template<typename _CharT, typename _Traits>
833 inline basic_ostream<_CharT, _Traits>&
834 emit_on_flush(basic_ostream<_CharT, _Traits>& __os)
836 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
841 template<typename _CharT, typename _Traits>
842 inline basic_ostream<_CharT, _Traits>&
843 noemit_on_flush(basic_ostream<_CharT, _Traits>& __os)
845 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
850 template<typename _CharT, typename _Traits>
851 inline basic_ostream<_CharT, _Traits>&
852 flush_emit(basic_ostream<_CharT, _Traits>& __os)
856 ~_Restore() { *_M_flag = _M_prev; }
858 bool _M_prev = false;
859 bool* _M_flag = &_M_prev;
862 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
864 __restore._M_prev = *__flag;
865 __restore._M_flag = __flag;
877_GLIBCXX_END_NAMESPACE_VERSION
880#include <bits/ostream.tcc>
882#endif /* _GLIBCXX_OSTREAM */