1// Output streams -*- C++ -*-
3// Copyright (C) 1997-2024 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>
42#if __cplusplus > 202002L
46# define __glibcxx_want_print
47#include <bits/version.h> // __glibcxx_syncbuf
49namespace std _GLIBCXX_VISIBILITY(default)
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
54 * @brief Template class basic_ostream.
57 * @tparam _CharT Type of character stream.
58 * @tparam _Traits Traits for character type, defaults to
59 * char_traits<_CharT>.
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.
65 template<typename _CharT, typename _Traits>
66 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
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;
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> >
82 typedef ctype<_CharT> __ctype_type;
85 * @brief Base constructor.
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.
92 basic_ostream(__streambuf_type* __sb)
96 * @brief Base destructor.
98 * This does very little apart from providing a virtual base dtor.
103 /// Safe prefix/suffix operations.
109 * @brief Interface for manipulators.
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.
116 operator<<(__ostream_type& (*__pf)(__ostream_type&))
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // DR 60. What is a formatted input function?
120 // The inserters for manipulators are *not* formatted output functions.
125 operator<<(__ios_type& (*__pf)(__ios_type&))
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // DR 60. What is a formatted input function?
129 // The inserters for manipulators are *not* formatted output functions.
135 operator<<(ios_base& (*__pf) (ios_base&))
137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 // DR 60. What is a formatted input function?
139 // The inserters for manipulators are *not* formatted output functions.
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.
155 * If the sentry status is good, the function tries to generate
156 * whatever data is appropriate for the type of the argument.
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
166 * @brief Integer arithmetic inserters
167 * @param __n A variable of builtin integral type.
168 * @return @c *this if successful
170 * These functions use the stream's current locale (specifically, the
171 * @c num_get facet) to perform numeric formatting.
175 { return _M_insert(__n); }
178 operator<<(unsigned long __n)
179 { return _M_insert(__n); }
183 { return _M_insert(__n); }
186 operator<<(short __n);
189 operator<<(unsigned short __n)
191 // _GLIBCXX_RESOLVE_LIB_DEFECTS
192 // 117. basic_ostream uses nonexistent num_put member functions.
193 return _M_insert(static_cast<unsigned long>(__n));
200 operator<<(unsigned int __n)
202 // _GLIBCXX_RESOLVE_LIB_DEFECTS
203 // 117. basic_ostream uses nonexistent num_put member functions.
204 return _M_insert(static_cast<unsigned long>(__n));
207#ifdef _GLIBCXX_USE_LONG_LONG
208#pragma GCC diagnostic push
209#pragma GCC diagnostic ignored "-Wlong-long"
211 operator<<(long long __n)
212 { return _M_insert(__n); }
215 operator<<(unsigned long long __n)
216 { return _M_insert(__n); }
217#pragma GCC diagnostic pop
223 * @brief Floating point arithmetic inserters
224 * @param __f A variable of builtin floating point type.
225 * @return @c *this if successful
227 * These functions use the stream's current locale (specifically, the
228 * @c num_get facet) to perform numeric formatting.
231 operator<<(double __f)
232 { return _M_insert(__f); }
235 operator<<(float __f)
237 // _GLIBCXX_RESOLVE_LIB_DEFECTS
238 // 117. basic_ostream uses nonexistent num_put member functions.
239 return _M_insert(static_cast<double>(__f));
243 operator<<(long double __f)
244 { return _M_insert(__f); }
247#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
248 __attribute__((__always_inline__))
250 operator<<(_Float16 __f)
252 return _M_insert(static_cast<double>(__f));
256#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
257 __attribute__((__always_inline__))
259 operator<<(_Float32 __f)
261 return _M_insert(static_cast<double>(__f));
265#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
266 __attribute__((__always_inline__))
268 operator<<(_Float64 __f)
270 return _M_insert(static_cast<double>(__f));
274#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
275 __attribute__((__always_inline__))
277 operator<<(_Float128 __f)
279 return _M_insert(static_cast<long double>(__f));
283#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
284 __attribute__((__always_inline__))
286 operator<<(__gnu_cxx::__bfloat16_t __f)
288 return _M_insert(static_cast<double>(__f));
293 * @brief Pointer arithmetic inserters
294 * @param __p A variable of pointer type.
295 * @return @c *this if successful
297 * These functions use the stream's current locale (specifically, the
298 * @c num_get facet) to perform numeric formatting.
301 operator<<(const void* __p)
302 { return _M_insert(__p); }
304#if __cplusplus >= 201703L
306 operator<<(nullptr_t)
307 { return *this << "nullptr"; }
310#if __cplusplus > 202002L
311 __attribute__((__always_inline__))
313 operator<<(const volatile void* __p)
314 { return _M_insert(const_cast<const void*>(__p)); }
318 * @brief Extracting from another streambuf.
319 * @param __sb A pointer to a streambuf
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
325 * If @p __sb is NULL, the stream will set failbit in its error state.
327 * Characters are extracted from @p __sb and inserted into @c *this
328 * until one of the following occurs:
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
336 * If the function inserts no characters, failbit is set.
339 operator<<(__streambuf_type* __sb);
344 * @name Unformatted Output Functions
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
352 * If the sentry status is good, the function tries to generate
353 * whatever data is appropriate for the type of the argument.
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.
362 * @brief Simple insertion.
363 * @param __c The character to insert.
366 * Tries to insert @p __c.
368 * @note This function is not overloaded on signed char and
375 * @brief Character string insertion.
376 * @param __s The array to insert.
377 * @param __n Maximum number of characters to insert.
380 * Characters are copied from @p __s and inserted into the stream until
381 * one of the following happens:
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)
387 * @note This function is not overloaded on signed char and
391 write(const char_type* __s, streamsize __n);
395 * @brief Synchronizing the stream buffer.
398 * If @c rdbuf() is a null pointer, changes nothing.
400 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
407 * @brief Getting the current write position.
408 * @return A file position object.
410 * If @c fail() is not false, returns @c pos_type(-1) to indicate
411 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
417 * @brief Changing the current write position.
418 * @param __pos A file position object.
421 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
422 * that function fails, sets failbit.
428 * @brief Changing the current write position.
429 * @param __off A file offset object.
430 * @param __dir The direction in which to seek.
433 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
434 * If that function fails, sets failbit.
437 seekp(off_type, ios_base::seekdir);
443#if __cplusplus >= 201103L
444 // Non-standard constructor that does not call init()
445 basic_ostream(basic_iostream<_CharT, _Traits>&) { }
447 basic_ostream(const basic_ostream&) = delete;
449 basic_ostream(basic_ostream&& __rhs)
451 { __ios_type::move(__rhs); }
453 // 27.7.3.3 Assign/swap
455 basic_ostream& operator=(const basic_ostream&) = delete;
458 operator=(basic_ostream&& __rhs)
465 swap(basic_ostream& __rhs)
466 { __ios_type::swap(__rhs); }
469 template<typename _ValueT>
471 _M_insert(_ValueT __v);
474#if !_GLIBCXX_INLINE_VERSION
476 _M_write(const char_type* __s, streamsize __n)
477 { std::__ostream_insert(*this, __s, __n); }
482 * @brief Performs setup work for output streams.
484 * Objects of this class are created before all of the standard
485 * inserters are run. It is responsible for <em>exception-safe prefix and
486 * suffix operations</em>.
488 template <typename _CharT, typename _Traits>
489 class basic_ostream<_CharT, _Traits>::sentry
493 basic_ostream<_CharT, _Traits>& _M_os;
497 * @brief The constructor performs preparatory work.
498 * @param __os The output stream to guard.
500 * If the stream state is good (@a __os.good() is true), then if the
501 * stream is tied to another output stream, @c is.tie()->flush()
502 * is called to synchronize the output sequences.
504 * If the stream state is still good, then the sentry state becomes
508 sentry(basic_ostream<_CharT, _Traits>& __os);
510#pragma GCC diagnostic push
511#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
513 * @brief Possibly flushes the stream.
515 * If @c ios_base::unitbuf is set in @c os.flags(), and
516 * @c std::uncaught_exception() is true, the sentry destructor calls
517 * @c flush() on the output stream.
522 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
524 // Can't call flush directly or else will get into recursive lock.
525 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
526 _M_os.setstate(ios_base::badbit);
529#pragma GCC diagnostic pop
532 * @brief Quick status checking.
533 * @return The sentry state.
535 * For ease of use, sentries may be converted to booleans. The
536 * return value is that of the sentry state (true == okay).
538#if __cplusplus >= 201103L
541 operator bool() const
547 * @brief Character inserters
548 * @param __out An output stream.
549 * @param __c A character.
552 * Behaves like one of the formatted arithmetic inserters described in
553 * std::basic_ostream. After constructing a sentry object with good
554 * status, this function inserts a single character and any required
555 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
558 * If @p __c is of type @c char and the character type of the stream is not
559 * @c char, the character is widened before insertion.
561 template<typename _CharT, typename _Traits>
562 inline basic_ostream<_CharT, _Traits>&
563 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
565 if (__out.width() != 0)
566 return __ostream_insert(__out, &__c, 1);
571 template<typename _CharT, typename _Traits>
572 inline basic_ostream<_CharT, _Traits>&
573 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
574 { return (__out << __out.widen(__c)); }
577 template<typename _Traits>
578 inline basic_ostream<char, _Traits>&
579 operator<<(basic_ostream<char, _Traits>& __out, char __c)
581 if (__out.width() != 0)
582 return __ostream_insert(__out, &__c, 1);
587 // Signed and unsigned
588 template<typename _Traits>
589 inline basic_ostream<char, _Traits>&
590 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
591 { return (__out << static_cast<char>(__c)); }
593 template<typename _Traits>
594 inline basic_ostream<char, _Traits>&
595 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
596 { return (__out << static_cast<char>(__c)); }
598#if __cplusplus > 201703L
599 // The following deleted overloads prevent formatting character values as
602 template<typename _Traits>
603 basic_ostream<char, _Traits>&
604 operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
606#ifdef _GLIBCXX_USE_CHAR8_T
607 template<typename _Traits>
608 basic_ostream<char, _Traits>&
609 operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
612 template<typename _Traits>
613 basic_ostream<char, _Traits>&
614 operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
616 template<typename _Traits>
617 basic_ostream<char, _Traits>&
618 operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
620#ifdef _GLIBCXX_USE_WCHAR_T
621#ifdef _GLIBCXX_USE_CHAR8_T
622 template<typename _Traits>
623 basic_ostream<wchar_t, _Traits>&
624 operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
625#endif // _GLIBCXX_USE_CHAR8_T
627 template<typename _Traits>
628 basic_ostream<wchar_t, _Traits>&
629 operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
631 template<typename _Traits>
632 basic_ostream<wchar_t, _Traits>&
633 operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
634#endif // _GLIBCXX_USE_WCHAR_T
640 * @brief String inserters
641 * @param __out An output stream.
642 * @param __s A character string.
644 * @pre @p __s must be a non-NULL pointer
646 * Behaves like one of the formatted arithmetic inserters described in
647 * std::basic_ostream. After constructing a sentry object with good
648 * status, this function inserts @c traits::length(__s) characters starting
649 * at @p __s, widened if necessary, followed by any required padding (as
650 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
652 template<typename _CharT, typename _Traits>
653 inline basic_ostream<_CharT, _Traits>&
654 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
657 __out.setstate(ios_base::badbit);
659 __ostream_insert(__out, __s,
660 static_cast<streamsize>(_Traits::length(__s)));
664 template<typename _CharT, typename _Traits>
665 basic_ostream<_CharT, _Traits> &
666 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
668 // Partial specializations
669 template<typename _Traits>
670 inline basic_ostream<char, _Traits>&
671 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
674 __out.setstate(ios_base::badbit);
676 __ostream_insert(__out, __s,
677 static_cast<streamsize>(_Traits::length(__s)));
681 // Signed and unsigned
682 template<typename _Traits>
683 inline basic_ostream<char, _Traits>&
684 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
685 { return (__out << reinterpret_cast<const char*>(__s)); }
687 template<typename _Traits>
688 inline basic_ostream<char, _Traits> &
689 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
690 { return (__out << reinterpret_cast<const char*>(__s)); }
692#if __cplusplus > 201703L
693 // The following deleted overloads prevent formatting strings as
696 template<typename _Traits>
697 basic_ostream<char, _Traits>&
698 operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
700#ifdef _GLIBCXX_USE_CHAR8_T
701 template<typename _Traits>
702 basic_ostream<char, _Traits>&
703 operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
704#endif // _GLIBCXX_USE_CHAR8_T
706 template<typename _Traits>
707 basic_ostream<char, _Traits>&
708 operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
710 template<typename _Traits>
711 basic_ostream<char, _Traits>&
712 operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
714#ifdef _GLIBCXX_USE_WCHAR_T
715#ifdef _GLIBCXX_USE_CHAR8_T
716 template<typename _Traits>
717 basic_ostream<wchar_t, _Traits>&
718 operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
721 template<typename _Traits>
722 basic_ostream<wchar_t, _Traits>&
723 operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
725 template<typename _Traits>
726 basic_ostream<wchar_t, _Traits>&
727 operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
728#endif // _GLIBCXX_USE_WCHAR_T
732 // Standard basic_ostream manipulators
735 * @brief Write a newline and flush the stream.
737 * This manipulator is often mistakenly used when a simple newline is
738 * desired, leading to poor buffering performance. See
739 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
740 * for more on this subject.
742 template<typename _CharT, typename _Traits>
743 inline basic_ostream<_CharT, _Traits>&
744 endl(basic_ostream<_CharT, _Traits>& __os)
745 { return flush(__os.put(__os.widen('\n'))); }
748 * @brief Write a null character into the output sequence.
750 * <em>Null character</em> is @c CharT() by definition. For CharT
751 * of @c char, this correctly writes the ASCII @c NUL character
754 template<typename _CharT, typename _Traits>
755 inline basic_ostream<_CharT, _Traits>&
756 ends(basic_ostream<_CharT, _Traits>& __os)
757 { return __os.put(_CharT()); }
760 * @brief Flushes the output stream.
762 * This manipulator simply calls the stream's @c flush() member function.
764 template<typename _CharT, typename _Traits>
765 inline basic_ostream<_CharT, _Traits>&
766 flush(basic_ostream<_CharT, _Traits>& __os)
767 { return __os.flush(); }
769#if __cplusplus >= 201103L
770 // C++11 27.7.3.9 Rvalue stream insertion [ostream.rvalue]
771 // _GLIBCXX_RESOLVE_LIB_DEFECTS
772 // 1203. More useful rvalue stream insertion
774#if __cpp_concepts >= 201907L && __glibcxx_type_trait_variable_templates
775 // Use concepts if possible because they're cheaper to evaluate.
776 template<typename _Tp>
777 concept __derived_from_ios_base = is_class_v<_Tp>
778 && (!is_same_v<_Tp, ios_base>)
779 && requires (_Tp* __t, ios_base* __b) { __b = __t; };
781 template<typename _Os, typename _Tp>
782 requires __derived_from_ios_base<_Os>
783 && requires (_Os& __os, const _Tp& __t) { __os << __t; }
784 using __rvalue_stream_insertion_t = _Os&&;
786 template<typename _Tp>
787 using _Require_derived_from_ios_base
788 = _Require<is_class<_Tp>, __not_<is_same<_Tp, ios_base>>,
789 is_convertible<typename add_pointer<_Tp>::type, ios_base*>>;
791 template<typename _Os, typename _Tp,
792 typename = _Require_derived_from_ios_base<_Os>,
794 = decltype(std::declval<_Os&>() << std::declval<const _Tp&>())>
795 using __rvalue_stream_insertion_t = _Os&&;
799 * @brief Generic inserter for rvalue stream
800 * @param __os An input stream.
801 * @param __x A reference to the object being inserted.
804 * This is just a forwarding function to allow insertion to
805 * rvalue streams since they won't bind to the inserter functions
806 * that take an lvalue reference.
808 template<typename _Ostream, typename _Tp>
809 inline __rvalue_stream_insertion_t<_Ostream, _Tp>
810 operator<<(_Ostream&& __os, const _Tp& __x)
813 return std::move(__os);
816#ifdef __glibcxx_syncbuf // C++ >= 20 && HOSTED && CXX11ABI
817 template<typename _CharT, typename _Traits>
818 class __syncbuf_base : public basic_streambuf<_CharT, _Traits>
822 _S_get(basic_streambuf<_CharT, _Traits>* __buf [[maybe_unused]]) noexcept
825 if (auto __p = dynamic_cast<__syncbuf_base*>(__buf))
826 return &__p->_M_emit_on_sync;
832 __syncbuf_base(basic_streambuf<_CharT, _Traits>* __w = nullptr)
836 basic_streambuf<_CharT, _Traits>* _M_wrapped = nullptr;
837 bool _M_emit_on_sync = false;
838 bool _M_needs_sync = false;
841 template<typename _CharT, typename _Traits>
842 inline basic_ostream<_CharT, _Traits>&
843 emit_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 noemit_on_flush(basic_ostream<_CharT, _Traits>& __os)
854 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
859 template<typename _CharT, typename _Traits>
860 inline basic_ostream<_CharT, _Traits>&
861 flush_emit(basic_ostream<_CharT, _Traits>& __os)
865 ~_Restore() { *_M_flag = _M_prev; }
867 bool _M_prev = false;
868 bool* _M_flag = &_M_prev;
871 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
873 __restore._M_prev = *__flag;
874 __restore._M_flag = __flag;
881#endif // __glibcxx_syncbuf
883#if __cpp_lib_print // C++ >= 23
886 vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args)
888 ostream::sentry __cerb(__os);
891 __format::_Str_sink<char> __buf;
892 std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
893 auto __out = __buf.view();
897 std::__ostream_write(__os, __out.data(), __out.size());
899 __catch(const __cxxabiv1::__forced_unwind&)
901 __os._M_setstate(ios_base::badbit);
902 __throw_exception_again;
905 { __os._M_setstate(ios_base::badbit); }
910 vprint_unicode(ostream& __os, string_view __fmt, format_args __args)
912#if !defined(_WIN32) || defined(__CYGWIN__)
913 // For most targets we don't need to do anything special to write
914 // Unicode to a terminal.
915 std::vprint_nonunicode(__os, __fmt, __args);
917 ostream::sentry __cerb(__os);
920 __format::_Str_sink<char> __buf;
921 std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
922 auto __out = __buf.view();
924 void* __open_terminal(streambuf*);
925 error_code __write_to_terminal(void*, span<char>);
926 // If stream refers to a terminal, write a Unicode string to it.
927 if (auto __term = __open_terminal(__os.rdbuf()))
929#if !defined(_WIN32) || defined(__CYGWIN__)
930 // For POSIX, __open_terminal(streambuf*) uses fdopen to open a
931 // new file, so we would need to close it here. This code is not
932 // actually compiled because it's inside an #ifdef _WIN32 group,
933 // but just in case that changes in future ...
936 _Guard(void* __p) : _M_f((FILE*)__p) { }
937 ~_Guard() { std::fclose(_M_f); }
938 _Guard(_Guard&&) = delete;
939 _Guard& operator=(_Guard&&) = delete;
945 ios_base::iostate __err = ios_base::goodbit;
948 if (__os.rdbuf()->pubsync() == -1)
950 else if (auto __e = __write_to_terminal(__term, __out))
951 if (__e != std::make_error_code(errc::illegal_byte_sequence))
954 __catch(const __cxxabiv1::__forced_unwind&)
956 __os._M_setstate(ios_base::badbit);
957 __throw_exception_again;
960 { __os._M_setstate(ios_base::badbit); }
963 __os.setstate(__err);
967 // Otherwise just insert the string as vprint_nonunicode does.
970 std::__ostream_write(__os, __out.data(), __out.size());
972 __catch(const __cxxabiv1::__forced_unwind&)
974 __os._M_setstate(ios_base::badbit);
975 __throw_exception_again;
978 { __os._M_setstate(ios_base::badbit); }
983 template<typename... _Args>
985 print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
987 auto __fmtargs = std::make_format_args(__args...);
988 if constexpr (__unicode::__literal_encoding_is_utf8())
989 std::vprint_unicode(__os, __fmt.get(), __fmtargs);
991 std::vprint_nonunicode(__os, __fmt.get(), __fmtargs);
994 template<typename... _Args>
996 println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
998 // _GLIBCXX_RESOLVE_LIB_DEFECTS
999 // 4088. println ignores the locale imbued in std::ostream
1000 std::print(__os, "{}\n", std::format(__os.getloc(), __fmt,
1001 std::forward<_Args>(__args)...));
1004 // Defined for C++26, supported as an extension to C++23.
1005 inline void println(ostream& __os)
1007#if defined(_WIN32) && !defined(__CYGWIN__)
1008 if constexpr (__unicode::__literal_encoding_is_utf8())
1009 std::vprint_unicode(__os, "\n", std::make_format_args());
1015#endif // __cpp_lib_print
1019_GLIBCXX_END_NAMESPACE_VERSION
1022#include <bits/ostream.tcc>
1024#endif /* _GLIBCXX_OSTREAM */