1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997-2026 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 #ifdef _GLIBCXX_SYSHDR
37 #pragma GCC system_header
40 #include <bits/requires_hosted.h> // iostreams
42 #include <bits/ostream.h>
44 #ifdef __glibcxx_print
45 # include <format> // format_string, make_format_args
48 #ifndef _GLIBCXX_NO_INLINE_PRINT
49 # include <bits/ostream_print.h>
52 # define __glibcxx_want_print
53 #include <bits/version.h> // __cpp_lib_print, __glibcxx_syncbuf
55 namespace std _GLIBCXX_VISIBILITY(default)
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 // Standard basic_ostream manipulators
62 * @brief Write a newline and flush the stream.
64 * This manipulator is often mistakenly used when a simple newline is
65 * desired, leading to poor buffering performance. See
66 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
67 * for more on this subject.
69 template<typename _CharT, typename _Traits>
70 inline basic_ostream<_CharT, _Traits>&
71 endl(basic_ostream<_CharT, _Traits>& __os)
72 { return flush(__os.put(__os.widen('\n'))); }
75 * @brief Write a null character into the output sequence.
77 * <em>Null character</em> is @c CharT() by definition. For CharT
78 * of @c char, this correctly writes the ASCII @c NUL character
81 template<typename _CharT, typename _Traits>
82 inline basic_ostream<_CharT, _Traits>&
83 ends(basic_ostream<_CharT, _Traits>& __os)
84 { return __os.put(_CharT()); }
87 * @brief Flushes the output stream.
89 * This manipulator simply calls the stream's @c flush() member function.
91 template<typename _CharT, typename _Traits>
92 inline basic_ostream<_CharT, _Traits>&
93 flush(basic_ostream<_CharT, _Traits>& __os)
94 { return __os.flush(); }
96 #ifdef __glibcxx_syncbuf // C++ >= 20 && HOSTED && CXX11ABI
97 template<typename _CharT, typename _Traits>
98 class __syncbuf_base : public basic_streambuf<_CharT, _Traits>
102 _S_get(basic_streambuf<_CharT, _Traits>* __buf [[maybe_unused]]) noexcept
105 if (auto __p = dynamic_cast<__syncbuf_base*>(__buf))
106 return &__p->_M_emit_on_sync;
112 __syncbuf_base(basic_streambuf<_CharT, _Traits>* __w = nullptr)
116 basic_streambuf<_CharT, _Traits>* _M_wrapped = nullptr;
117 bool _M_emit_on_sync = false;
118 bool _M_needs_sync = false;
121 template<typename _CharT, typename _Traits>
122 inline basic_ostream<_CharT, _Traits>&
123 emit_on_flush(basic_ostream<_CharT, _Traits>& __os)
125 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
130 template<typename _CharT, typename _Traits>
131 inline basic_ostream<_CharT, _Traits>&
132 noemit_on_flush(basic_ostream<_CharT, _Traits>& __os)
134 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
139 template<typename _CharT, typename _Traits>
140 inline basic_ostream<_CharT, _Traits>&
141 flush_emit(basic_ostream<_CharT, _Traits>& __os)
145 ~_Restore() { *_M_flag = _M_prev; }
147 bool _M_prev = false;
148 bool* _M_flag = &_M_prev;
151 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
153 __restore._M_prev = *__flag;
154 __restore._M_flag = __flag;
161 #endif // __glibcxx_syncbuf
163 #if __cpp_lib_print // C++ >= 23
165 vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args);
168 vprint_unicode(ostream& __os, string_view __fmt, format_args __args);
170 template<typename... _Args>
172 print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
174 auto __fmtargs = std::make_format_args(__args...);
175 #if defined(_WIN32) && !defined(__CYGWIN__)
176 if constexpr (__unicode::__literal_encoding_is_utf8())
177 std::vprint_unicode(__os, __fmt.get(), __fmtargs);
180 std::vprint_nonunicode(__os, __fmt.get(), __fmtargs);
183 template<typename... _Args>
185 println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
187 auto __fmtargs = std::make_format_args(__args...);
189 __fmtn.reserve(__fmt.get().size() + 1);
190 __fmtn = __fmt.get();
192 #if defined(_WIN32) && !defined(__CYGWIN__)
193 if constexpr (__unicode::__literal_encoding_is_utf8())
194 std::vprint_unicode(__os, __fmtn, __fmtargs);
197 std::vprint_nonunicode(__os, __fmtn, __fmtargs);
200 // Defined for C++26, supported as an extension to C++23.
201 inline void println(ostream& __os)
203 #if defined(_WIN32) && !defined(__CYGWIN__)
204 if constexpr (__unicode::__literal_encoding_is_utf8())
205 std::vprint_unicode(__os, "\n", std::make_format_args());
210 #endif // __cpp_lib_print
212 _GLIBCXX_END_NAMESPACE_VERSION
215 #include <bits/ostream.tcc>
217 #endif /* _GLIBCXX_OSTREAM */