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
49 namespace 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
209 operator<<(long long __n)
210 { return _M_insert(__n); }
213 operator<<(unsigned long long __n)
214 { return _M_insert(__n); }
220 * @brief Floating point arithmetic inserters
221 * @param __f A variable of builtin floating point type.
222 * @return @c *this if successful
224 * These functions use the stream's current locale (specifically, the
225 * @c num_get facet) to perform numeric formatting.
228 operator<<(double __f)
229 { return _M_insert(__f); }
232 operator<<(float __f)
234 // _GLIBCXX_RESOLVE_LIB_DEFECTS
235 // 117. basic_ostream uses nonexistent num_put member functions.
236 return _M_insert(static_cast<double>(__f));
240 operator<<(long double __f)
241 { return _M_insert(__f); }
244 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
245 __attribute__((__always_inline__))
247 operator<<(_Float16 __f)
249 return _M_insert(static_cast<double>(__f));
253 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
254 __attribute__((__always_inline__))
256 operator<<(_Float32 __f)
258 return _M_insert(static_cast<double>(__f));
262 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
263 __attribute__((__always_inline__))
265 operator<<(_Float64 __f)
267 return _M_insert(static_cast<double>(__f));
271 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
272 __attribute__((__always_inline__))
274 operator<<(_Float128 __f)
276 return _M_insert(static_cast<long double>(__f));
280 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
281 __attribute__((__always_inline__))
283 operator<<(__gnu_cxx::__bfloat16_t __f)
285 return _M_insert(static_cast<double>(__f));
290 * @brief Pointer arithmetic inserters
291 * @param __p A variable of pointer type.
292 * @return @c *this if successful
294 * These functions use the stream's current locale (specifically, the
295 * @c num_get facet) to perform numeric formatting.
298 operator<<(const void* __p)
299 { return _M_insert(__p); }
301 #if __cplusplus >= 201703L
303 operator<<(nullptr_t)
304 { return *this << "nullptr"; }
307 #if __cplusplus > 202002L
308 __attribute__((__always_inline__))
310 operator<<(const volatile void* __p)
311 { return _M_insert(const_cast<const void*>(__p)); }
315 * @brief Extracting from another streambuf.
316 * @param __sb A pointer to a streambuf
318 * This function behaves like one of the basic arithmetic extractors,
319 * in that it also constructs a sentry object and has the same error
322 * If @p __sb is NULL, the stream will set failbit in its error state.
324 * Characters are extracted from @p __sb and inserted into @c *this
325 * until one of the following occurs:
327 * - the input stream reaches end-of-file,
328 * - insertion into the output sequence fails (in this case, the
329 * character that would have been inserted is not extracted), or
330 * - an exception occurs while getting a character from @p __sb, which
331 * sets failbit in the error state
333 * If the function inserts no characters, failbit is set.
336 operator<<(__streambuf_type* __sb);
341 * @name Unformatted Output Functions
343 * All the unformatted output functions have some common behavior.
344 * Each starts by constructing a temporary object of type
345 * std::basic_ostream::sentry. This has several effects, concluding
346 * with the setting of a status flag; see the sentry documentation
349 * If the sentry status is good, the function tries to generate
350 * whatever data is appropriate for the type of the argument.
352 * If an exception is thrown during insertion, ios_base::badbit
353 * will be turned on in the stream's error state. If badbit is on in
354 * the stream's exceptions mask, the exception will be rethrown
355 * without completing its actions.
359 * @brief Simple insertion.
360 * @param __c The character to insert.
363 * Tries to insert @p __c.
365 * @note This function is not overloaded on signed char and
372 * @brief Character string insertion.
373 * @param __s The array to insert.
374 * @param __n Maximum number of characters to insert.
377 * Characters are copied from @p __s and inserted into the stream until
378 * one of the following happens:
380 * - @p __n characters are inserted
381 * - inserting into the output sequence fails (in this case, badbit
382 * will be set in the stream's error state)
384 * @note This function is not overloaded on signed char and
388 write(const char_type* __s, streamsize __n);
392 * @brief Synchronizing the stream buffer.
395 * If @c rdbuf() is a null pointer, changes nothing.
397 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
404 * @brief Getting the current write position.
405 * @return A file position object.
407 * If @c fail() is not false, returns @c pos_type(-1) to indicate
408 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
414 * @brief Changing the current write position.
415 * @param __pos A file position object.
418 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
419 * that function fails, sets failbit.
425 * @brief Changing the current write position.
426 * @param __off A file offset object.
427 * @param __dir The direction in which to seek.
430 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
431 * If that function fails, sets failbit.
434 seekp(off_type, ios_base::seekdir);
440 #if __cplusplus >= 201103L
441 // Non-standard constructor that does not call init()
442 basic_ostream(basic_iostream<_CharT, _Traits>&) { }
444 basic_ostream(const basic_ostream&) = delete;
446 basic_ostream(basic_ostream&& __rhs)
448 { __ios_type::move(__rhs); }
450 // 27.7.3.3 Assign/swap
452 basic_ostream& operator=(const basic_ostream&) = delete;
455 operator=(basic_ostream&& __rhs)
462 swap(basic_ostream& __rhs)
463 { __ios_type::swap(__rhs); }
466 template<typename _ValueT>
468 _M_insert(_ValueT __v);
471 #if !_GLIBCXX_INLINE_VERSION
473 _M_write(const char_type* __s, streamsize __n)
474 { std::__ostream_insert(*this, __s, __n); }
479 * @brief Performs setup work for output streams.
481 * Objects of this class are created before all of the standard
482 * inserters are run. It is responsible for <em>exception-safe prefix and
483 * suffix operations</em>.
485 template <typename _CharT, typename _Traits>
486 class basic_ostream<_CharT, _Traits>::sentry
490 basic_ostream<_CharT, _Traits>& _M_os;
494 * @brief The constructor performs preparatory work.
495 * @param __os The output stream to guard.
497 * If the stream state is good (@a __os.good() is true), then if the
498 * stream is tied to another output stream, @c is.tie()->flush()
499 * is called to synchronize the output sequences.
501 * If the stream state is still good, then the sentry state becomes
505 sentry(basic_ostream<_CharT, _Traits>& __os);
507 #pragma GCC diagnostic push
508 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
510 * @brief Possibly flushes the stream.
512 * If @c ios_base::unitbuf is set in @c os.flags(), and
513 * @c std::uncaught_exception() is true, the sentry destructor calls
514 * @c flush() on the output stream.
519 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
521 // Can't call flush directly or else will get into recursive lock.
522 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
523 _M_os.setstate(ios_base::badbit);
526 #pragma GCC diagnostic pop
529 * @brief Quick status checking.
530 * @return The sentry state.
532 * For ease of use, sentries may be converted to booleans. The
533 * return value is that of the sentry state (true == okay).
535 #if __cplusplus >= 201103L
538 operator bool() const
544 * @brief Character inserters
545 * @param __out An output stream.
546 * @param __c A character.
549 * Behaves like one of the formatted arithmetic inserters described in
550 * std::basic_ostream. After constructing a sentry object with good
551 * status, this function inserts a single character and any required
552 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
555 * If @p __c is of type @c char and the character type of the stream is not
556 * @c char, the character is widened before insertion.
558 template<typename _CharT, typename _Traits>
559 inline basic_ostream<_CharT, _Traits>&
560 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
562 if (__out.width() != 0)
563 return __ostream_insert(__out, &__c, 1);
568 template<typename _CharT, typename _Traits>
569 inline basic_ostream<_CharT, _Traits>&
570 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
571 { return (__out << __out.widen(__c)); }
574 template<typename _Traits>
575 inline basic_ostream<char, _Traits>&
576 operator<<(basic_ostream<char, _Traits>& __out, char __c)
578 if (__out.width() != 0)
579 return __ostream_insert(__out, &__c, 1);
584 // Signed and unsigned
585 template<typename _Traits>
586 inline basic_ostream<char, _Traits>&
587 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
588 { return (__out << static_cast<char>(__c)); }
590 template<typename _Traits>
591 inline basic_ostream<char, _Traits>&
592 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
593 { return (__out << static_cast<char>(__c)); }
595 #if __cplusplus > 201703L
596 // The following deleted overloads prevent formatting character values as
599 template<typename _Traits>
600 basic_ostream<char, _Traits>&
601 operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
603 #ifdef _GLIBCXX_USE_CHAR8_T
604 template<typename _Traits>
605 basic_ostream<char, _Traits>&
606 operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
609 template<typename _Traits>
610 basic_ostream<char, _Traits>&
611 operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
613 template<typename _Traits>
614 basic_ostream<char, _Traits>&
615 operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
617 #ifdef _GLIBCXX_USE_WCHAR_T
618 #ifdef _GLIBCXX_USE_CHAR8_T
619 template<typename _Traits>
620 basic_ostream<wchar_t, _Traits>&
621 operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
622 #endif // _GLIBCXX_USE_CHAR8_T
624 template<typename _Traits>
625 basic_ostream<wchar_t, _Traits>&
626 operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
628 template<typename _Traits>
629 basic_ostream<wchar_t, _Traits>&
630 operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
631 #endif // _GLIBCXX_USE_WCHAR_T
637 * @brief String inserters
638 * @param __out An output stream.
639 * @param __s A character string.
641 * @pre @p __s must be a non-NULL pointer
643 * Behaves like one of the formatted arithmetic inserters described in
644 * std::basic_ostream. After constructing a sentry object with good
645 * status, this function inserts @c traits::length(__s) characters starting
646 * at @p __s, widened if necessary, followed by any required padding (as
647 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
649 template<typename _CharT, typename _Traits>
650 inline basic_ostream<_CharT, _Traits>&
651 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
654 __out.setstate(ios_base::badbit);
656 __ostream_insert(__out, __s,
657 static_cast<streamsize>(_Traits::length(__s)));
661 template<typename _CharT, typename _Traits>
662 basic_ostream<_CharT, _Traits> &
663 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
665 // Partial specializations
666 template<typename _Traits>
667 inline basic_ostream<char, _Traits>&
668 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
671 __out.setstate(ios_base::badbit);
673 __ostream_insert(__out, __s,
674 static_cast<streamsize>(_Traits::length(__s)));
678 // Signed and unsigned
679 template<typename _Traits>
680 inline basic_ostream<char, _Traits>&
681 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
682 { return (__out << reinterpret_cast<const char*>(__s)); }
684 template<typename _Traits>
685 inline basic_ostream<char, _Traits> &
686 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
687 { return (__out << reinterpret_cast<const char*>(__s)); }
689 #if __cplusplus > 201703L
690 // The following deleted overloads prevent formatting strings as
693 template<typename _Traits>
694 basic_ostream<char, _Traits>&
695 operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
697 #ifdef _GLIBCXX_USE_CHAR8_T
698 template<typename _Traits>
699 basic_ostream<char, _Traits>&
700 operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
701 #endif // _GLIBCXX_USE_CHAR8_T
703 template<typename _Traits>
704 basic_ostream<char, _Traits>&
705 operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
707 template<typename _Traits>
708 basic_ostream<char, _Traits>&
709 operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
711 #ifdef _GLIBCXX_USE_WCHAR_T
712 #ifdef _GLIBCXX_USE_CHAR8_T
713 template<typename _Traits>
714 basic_ostream<wchar_t, _Traits>&
715 operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
718 template<typename _Traits>
719 basic_ostream<wchar_t, _Traits>&
720 operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
722 template<typename _Traits>
723 basic_ostream<wchar_t, _Traits>&
724 operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
725 #endif // _GLIBCXX_USE_WCHAR_T
729 // Standard basic_ostream manipulators
732 * @brief Write a newline and flush the stream.
734 * This manipulator is often mistakenly used when a simple newline is
735 * desired, leading to poor buffering performance. See
736 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
737 * for more on this subject.
739 template<typename _CharT, typename _Traits>
740 inline basic_ostream<_CharT, _Traits>&
741 endl(basic_ostream<_CharT, _Traits>& __os)
742 { return flush(__os.put(__os.widen('\n'))); }
745 * @brief Write a null character into the output sequence.
747 * <em>Null character</em> is @c CharT() by definition. For CharT
748 * of @c char, this correctly writes the ASCII @c NUL character
751 template<typename _CharT, typename _Traits>
752 inline basic_ostream<_CharT, _Traits>&
753 ends(basic_ostream<_CharT, _Traits>& __os)
754 { return __os.put(_CharT()); }
757 * @brief Flushes the output stream.
759 * This manipulator simply calls the stream's @c flush() member function.
761 template<typename _CharT, typename _Traits>
762 inline basic_ostream<_CharT, _Traits>&
763 flush(basic_ostream<_CharT, _Traits>& __os)
764 { return __os.flush(); }
766 #if __cplusplus >= 201103L
767 // C++11 27.7.3.9 Rvalue stream insertion [ostream.rvalue]
768 // _GLIBCXX_RESOLVE_LIB_DEFECTS
769 // 1203. More useful rvalue stream insertion
771 #if __cpp_concepts >= 201907L
772 // Use concepts if possible because they're cheaper to evaluate.
773 template<typename _Tp>
774 concept __derived_from_ios_base = is_class_v<_Tp>
775 && (!is_same_v<_Tp, ios_base>)
776 && requires (_Tp* __t, ios_base* __b) { __b = __t; };
778 template<typename _Os, typename _Tp>
779 requires __derived_from_ios_base<_Os>
780 && requires (_Os& __os, const _Tp& __t) { __os << __t; }
781 using __rvalue_stream_insertion_t = _Os&&;
783 template<typename _Tp>
784 using _Require_derived_from_ios_base
785 = _Require<is_class<_Tp>, __not_<is_same<_Tp, ios_base>>,
786 is_convertible<typename add_pointer<_Tp>::type, ios_base*>>;
788 template<typename _Os, typename _Tp,
789 typename = _Require_derived_from_ios_base<_Os>,
791 = decltype(std::declval<_Os&>() << std::declval<const _Tp&>())>
792 using __rvalue_stream_insertion_t = _Os&&;
796 * @brief Generic inserter for rvalue stream
797 * @param __os An input stream.
798 * @param __x A reference to the object being inserted.
801 * This is just a forwarding function to allow insertion to
802 * rvalue streams since they won't bind to the inserter functions
803 * that take an lvalue reference.
805 template<typename _Ostream, typename _Tp>
806 inline __rvalue_stream_insertion_t<_Ostream, _Tp>
807 operator<<(_Ostream&& __os, const _Tp& __x)
810 return std::move(__os);
813 #ifdef __glibcxx_syncbuf // C++ >= 20 && HOSTED && CXX11ABI
814 template<typename _CharT, typename _Traits>
815 class __syncbuf_base : public basic_streambuf<_CharT, _Traits>
819 _S_get(basic_streambuf<_CharT, _Traits>* __buf [[maybe_unused]]) noexcept
822 if (auto __p = dynamic_cast<__syncbuf_base*>(__buf))
823 return &__p->_M_emit_on_sync;
829 __syncbuf_base(basic_streambuf<_CharT, _Traits>* __w = nullptr)
833 basic_streambuf<_CharT, _Traits>* _M_wrapped = nullptr;
834 bool _M_emit_on_sync = false;
835 bool _M_needs_sync = false;
838 template<typename _CharT, typename _Traits>
839 inline basic_ostream<_CharT, _Traits>&
840 emit_on_flush(basic_ostream<_CharT, _Traits>& __os)
842 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
847 template<typename _CharT, typename _Traits>
848 inline basic_ostream<_CharT, _Traits>&
849 noemit_on_flush(basic_ostream<_CharT, _Traits>& __os)
851 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
856 template<typename _CharT, typename _Traits>
857 inline basic_ostream<_CharT, _Traits>&
858 flush_emit(basic_ostream<_CharT, _Traits>& __os)
862 ~_Restore() { *_M_flag = _M_prev; }
864 bool _M_prev = false;
865 bool* _M_flag = &_M_prev;
868 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
870 __restore._M_prev = *__flag;
871 __restore._M_flag = __flag;
878 #endif // __glibcxx_syncbuf
880 #if __cpp_lib_print // C++ >= 23
883 vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args)
885 ostream::sentry __cerb(__os);
888 __format::_Str_sink<char> __buf;
889 std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
890 auto __out = __buf.view();
894 std::__ostream_write(__os, __out.data(), __out.size());
896 __catch(const __cxxabiv1::__forced_unwind&)
898 __os._M_setstate(ios_base::badbit);
899 __throw_exception_again;
902 { __os._M_setstate(ios_base::badbit); }
907 vprint_unicode(ostream& __os, string_view __fmt, format_args __args)
910 // For most targets we don't need to do anything special to write
911 // Unicode to a terminal.
912 std::vprint_nonunicode(__os, __fmt, __args);
914 ostream::sentry __cerb(__os);
917 __format::_Str_sink<char> __buf;
918 std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
919 auto __out = __buf.view();
921 void* __open_terminal(streambuf*);
922 error_code __write_to_terminal(void*, span<char>);
923 // If stream refers to a terminal, write a Unicode string to it.
924 if (auto __term = __open_terminal(__os.rdbuf()))
927 // For POSIX, __open_terminal(streambuf*) uses fdopen to open a
928 // new file, so we would need to close it here. This code is not
929 // actually compiled because it's inside an #ifdef _WIN32 group,
930 // but just in case that changes in future ...
933 _Guard(void* __p) : _M_f((FILE*)__p) { }
934 ~_Guard() { std::fclose(_M_f); }
935 _Guard(_Guard&&) = delete;
936 _Guard& operator=(_Guard&&) = delete;
942 ios_base::iostate __err = ios_base::goodbit;
945 if (__os.rdbuf()->pubsync() == -1)
947 else if (auto __e = __write_to_terminal(__term, __out))
948 if (__e != std::make_error_code(errc::illegal_byte_sequence))
951 __catch(const __cxxabiv1::__forced_unwind&)
953 __os._M_setstate(ios_base::badbit);
954 __throw_exception_again;
957 { __os._M_setstate(ios_base::badbit); }
960 __os.setstate(__err);
964 // Otherwise just insert the string as vprint_nonunicode does.
967 std::__ostream_write(__os, __out.data(), __out.size());
969 __catch(const __cxxabiv1::__forced_unwind&)
971 __os._M_setstate(ios_base::badbit);
972 __throw_exception_again;
975 { __os._M_setstate(ios_base::badbit); }
980 template<typename... _Args>
982 print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
984 auto __fmtargs = std::make_format_args(std::forward<_Args>(__args)...);
985 if constexpr (__format::__literal_encoding_is_utf8())
986 std::vprint_unicode(__os, __fmt.get(), __fmtargs);
988 std::vprint_nonunicode(__os, __fmt.get(), __fmtargs);
991 template<typename... _Args>
993 println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
995 std::print(__os, "{}\n",
996 std::format(__fmt, std::forward<_Args>(__args)...));
998 #endif // __cpp_lib_print
1002 _GLIBCXX_END_NAMESPACE_VERSION
1005 #include <bits/ostream.tcc>
1007 #endif /* _GLIBCXX_OSTREAM */