libstdc++: ostream.h Source File

libstdc++

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