1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997-2020 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
39 #include <bits/ostream_insert.h>
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 * @brief Template class basic_ostream.
49 * @tparam _CharT Type of character stream.
50 * @tparam _Traits Traits for character type, defaults to
51 * char_traits<_CharT>.
53 * This is the base class for all output streams. It provides text
54 * formatting of all builtin types, and communicates with any class
55 * derived from basic_streambuf to do the actual output.
57 template<typename _CharT, typename _Traits>
58 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
61 // Types (inherited from basic_ios):
62 typedef _CharT char_type;
63 typedef typename _Traits::int_type int_type;
64 typedef typename _Traits::pos_type pos_type;
65 typedef typename _Traits::off_type off_type;
66 typedef _Traits traits_type;
68 // Non-standard Types:
69 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
70 typedef basic_ios<_CharT, _Traits> __ios_type;
71 typedef basic_ostream<_CharT, _Traits> __ostream_type;
72 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
74 typedef ctype<_CharT> __ctype_type;
77 * @brief Base constructor.
79 * This ctor is almost never called by the user directly, rather from
80 * derived classes' initialization lists, which pass a pointer to
81 * their own stream buffer.
84 basic_ostream(__streambuf_type* __sb)
88 * @brief Base destructor.
90 * This does very little apart from providing a virtual base dtor.
95 /// Safe prefix/suffix operations.
101 * @brief Interface for manipulators.
103 * Manipulators such as @c std::endl and @c std::hex use these
104 * functions in constructs like "std::cout << std::endl". For more
105 * information, see the iomanip header.
108 operator<<(__ostream_type& (*__pf)(__ostream_type&))
110 // _GLIBCXX_RESOLVE_LIB_DEFECTS
111 // DR 60. What is a formatted input function?
112 // The inserters for manipulators are *not* formatted output functions.
117 operator<<(__ios_type& (*__pf)(__ios_type&))
119 // _GLIBCXX_RESOLVE_LIB_DEFECTS
120 // DR 60. What is a formatted input function?
121 // The inserters for manipulators are *not* formatted output functions.
127 operator<<(ios_base& (*__pf) (ios_base&))
129 // _GLIBCXX_RESOLVE_LIB_DEFECTS
130 // DR 60. What is a formatted input function?
131 // The inserters for manipulators are *not* formatted output functions.
141 * All the @c operator<< functions (aka <em>formatted output
142 * functions</em>) have some common behavior. Each starts by
143 * constructing a temporary object of type std::basic_ostream::sentry.
144 * This can have several effects, concluding with the setting of a
145 * status flag; see the sentry documentation for more.
147 * If the sentry status is good, the function tries to generate
148 * whatever data is appropriate for the type of the argument.
150 * If an exception is thrown during insertion, ios_base::badbit
151 * will be turned on in the stream's error state without causing an
152 * ios_base::failure to be thrown. The original exception will then
158 * @brief Integer arithmetic inserters
159 * @param __n A variable of builtin integral type.
160 * @return @c *this if successful
162 * These functions use the stream's current locale (specifically, the
163 * @c num_get facet) to perform numeric formatting.
167 { return _M_insert(__n); }
170 operator<<(unsigned long __n)
171 { return _M_insert(__n); }
175 { return _M_insert(__n); }
178 operator<<(short __n);
181 operator<<(unsigned short __n)
183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
184 // 117. basic_ostream uses nonexistent num_put member functions.
185 return _M_insert(static_cast<unsigned long>(__n));
192 operator<<(unsigned int __n)
194 // _GLIBCXX_RESOLVE_LIB_DEFECTS
195 // 117. basic_ostream uses nonexistent num_put member functions.
196 return _M_insert(static_cast<unsigned long>(__n));
199 #ifdef _GLIBCXX_USE_LONG_LONG
201 operator<<(long long __n)
202 { return _M_insert(__n); }
205 operator<<(unsigned long long __n)
206 { return _M_insert(__n); }
212 * @brief Floating point arithmetic inserters
213 * @param __f A variable of builtin floating point type.
214 * @return @c *this if successful
216 * These functions use the stream's current locale (specifically, the
217 * @c num_get facet) to perform numeric formatting.
220 operator<<(double __f)
221 { return _M_insert(__f); }
224 operator<<(float __f)
226 // _GLIBCXX_RESOLVE_LIB_DEFECTS
227 // 117. basic_ostream uses nonexistent num_put member functions.
228 return _M_insert(static_cast<double>(__f));
232 operator<<(long double __f)
233 { return _M_insert(__f); }
237 * @brief Pointer arithmetic inserters
238 * @param __p A variable of pointer type.
239 * @return @c *this if successful
241 * These functions use the stream's current locale (specifically, the
242 * @c num_get facet) to perform numeric formatting.
245 operator<<(const void* __p)
246 { return _M_insert(__p); }
248 #if __cplusplus >= 201703L
250 operator<<(nullptr_t)
251 { return *this << "nullptr"; }
255 * @brief Extracting from another streambuf.
256 * @param __sb A pointer to a streambuf
258 * This function behaves like one of the basic arithmetic extractors,
259 * in that it also constructs a sentry object and has the same error
262 * If @p __sb is NULL, the stream will set failbit in its error state.
264 * Characters are extracted from @p __sb and inserted into @c *this
265 * until one of the following occurs:
267 * - the input stream reaches end-of-file,
268 * - insertion into the output sequence fails (in this case, the
269 * character that would have been inserted is not extracted), or
270 * - an exception occurs while getting a character from @p __sb, which
271 * sets failbit in the error state
273 * If the function inserts no characters, failbit is set.
276 operator<<(__streambuf_type* __sb);
281 * @name Unformatted Output Functions
283 * All the unformatted output functions have some common behavior.
284 * Each starts by constructing a temporary object of type
285 * std::basic_ostream::sentry. This has several effects, concluding
286 * with the setting of a status flag; see the sentry documentation
289 * If the sentry status is good, the function tries to generate
290 * whatever data is appropriate for the type of the argument.
292 * If an exception is thrown during insertion, ios_base::badbit
293 * will be turned on in the stream's error state. If badbit is on in
294 * the stream's exceptions mask, the exception will be rethrown
295 * without completing its actions.
299 * @brief Simple insertion.
300 * @param __c The character to insert.
303 * Tries to insert @p __c.
305 * @note This function is not overloaded on signed char and
312 * @brief Core write functionality, without sentry.
313 * @param __s The array to insert.
314 * @param __n Maximum number of characters to insert.
317 _M_write(const char_type* __s, streamsize __n)
319 const streamsize __put = this->rdbuf()->sputn(__s, __n);
321 this->setstate(ios_base::badbit);
325 * @brief Character string insertion.
326 * @param __s The array to insert.
327 * @param __n Maximum number of characters to insert.
330 * Characters are copied from @p __s and inserted into the stream until
331 * one of the following happens:
333 * - @p __n characters are inserted
334 * - inserting into the output sequence fails (in this case, badbit
335 * will be set in the stream's error state)
337 * @note This function is not overloaded on signed char and
341 write(const char_type* __s, streamsize __n);
345 * @brief Synchronizing the stream buffer.
348 * If @c rdbuf() is a null pointer, changes nothing.
350 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
357 * @brief Getting the current write position.
358 * @return A file position object.
360 * If @c fail() is not false, returns @c pos_type(-1) to indicate
361 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
367 * @brief Changing the current write position.
368 * @param __pos A file position object.
371 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
372 * that function fails, sets failbit.
378 * @brief Changing the current write position.
379 * @param __off A file offset object.
380 * @param __dir The direction in which to seek.
383 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
384 * If that function fails, sets failbit.
387 seekp(off_type, ios_base::seekdir);
393 #if __cplusplus >= 201103L
394 // Non-standard constructor that does not call init()
395 basic_ostream(basic_iostream<_CharT, _Traits>&) { }
397 basic_ostream(const basic_ostream&) = delete;
399 basic_ostream(basic_ostream&& __rhs)
401 { __ios_type::move(__rhs); }
403 // 27.7.3.3 Assign/swap
405 basic_ostream& operator=(const basic_ostream&) = delete;
408 operator=(basic_ostream&& __rhs)
415 swap(basic_ostream& __rhs)
416 { __ios_type::swap(__rhs); }
419 template<typename _ValueT>
421 _M_insert(_ValueT __v);
425 * @brief Performs setup work for output streams.
427 * Objects of this class are created before all of the standard
428 * inserters are run. It is responsible for <em>exception-safe prefix and
429 * suffix operations</em>.
431 template <typename _CharT, typename _Traits>
432 class basic_ostream<_CharT, _Traits>::sentry
436 basic_ostream<_CharT, _Traits>& _M_os;
440 * @brief The constructor performs preparatory work.
441 * @param __os The output stream to guard.
443 * If the stream state is good (@a __os.good() is true), then if the
444 * stream is tied to another output stream, @c is.tie()->flush()
445 * is called to synchronize the output sequences.
447 * If the stream state is still good, then the sentry state becomes
451 sentry(basic_ostream<_CharT, _Traits>& __os);
453 #pragma GCC diagnostic push
454 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
456 * @brief Possibly flushes the stream.
458 * If @c ios_base::unitbuf is set in @c os.flags(), and
459 * @c std::uncaught_exception() is true, the sentry destructor calls
460 * @c flush() on the output stream.
465 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
467 // Can't call flush directly or else will get into recursive lock.
468 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
469 _M_os.setstate(ios_base::badbit);
472 #pragma GCC diagnostic pop
475 * @brief Quick status checking.
476 * @return The sentry state.
478 * For ease of use, sentries may be converted to booleans. The
479 * return value is that of the sentry state (true == okay).
481 #if __cplusplus >= 201103L
484 operator bool() const
490 * @brief Character inserters
491 * @param __out An output stream.
492 * @param __c A character.
495 * Behaves like one of the formatted arithmetic inserters described in
496 * std::basic_ostream. After constructing a sentry object with good
497 * status, this function inserts a single character and any required
498 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
501 * If @p __c is of type @c char and the character type of the stream is not
502 * @c char, the character is widened before insertion.
504 template<typename _CharT, typename _Traits>
505 inline basic_ostream<_CharT, _Traits>&
506 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
507 { return __ostream_insert(__out, &__c, 1); }
509 template<typename _CharT, typename _Traits>
510 inline basic_ostream<_CharT, _Traits>&
511 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
512 { return (__out << __out.widen(__c)); }
515 template<typename _Traits>
516 inline basic_ostream<char, _Traits>&
517 operator<<(basic_ostream<char, _Traits>& __out, char __c)
518 { return __ostream_insert(__out, &__c, 1); }
520 // Signed and unsigned
521 template<typename _Traits>
522 inline basic_ostream<char, _Traits>&
523 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
524 { return (__out << static_cast<char>(__c)); }
526 template<typename _Traits>
527 inline basic_ostream<char, _Traits>&
528 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
529 { return (__out << static_cast<char>(__c)); }
531 #if __cplusplus > 201703L
532 // The following deleted overloads prevent formatting character values as
535 #ifdef _GLIBCXX_USE_WCHAR_T
536 template<typename _Traits>
537 basic_ostream<char, _Traits>&
538 operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
539 #endif // _GLIBCXX_USE_WCHAR_T
541 #ifdef _GLIBCXX_USE_CHAR8_T
542 template<typename _Traits>
543 basic_ostream<char, _Traits>&
544 operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
547 template<typename _Traits>
548 basic_ostream<char, _Traits>&
549 operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
551 template<typename _Traits>
552 basic_ostream<char, _Traits>&
553 operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
555 #ifdef _GLIBCXX_USE_WCHAR_T
556 #ifdef _GLIBCXX_USE_CHAR8_T
557 template<typename _Traits>
558 basic_ostream<wchar_t, _Traits>&
559 operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
560 #endif // _GLIBCXX_USE_CHAR8_T
562 template<typename _Traits>
563 basic_ostream<wchar_t, _Traits>&
564 operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
566 template<typename _Traits>
567 basic_ostream<wchar_t, _Traits>&
568 operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
569 #endif // _GLIBCXX_USE_WCHAR_T
575 * @brief String inserters
576 * @param __out An output stream.
577 * @param __s A character string.
579 * @pre @p __s must be a non-NULL pointer
581 * Behaves like one of the formatted arithmetic inserters described in
582 * std::basic_ostream. After constructing a sentry object with good
583 * status, this function inserts @c traits::length(__s) characters starting
584 * at @p __s, widened if necessary, followed by any required padding (as
585 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
587 template<typename _CharT, typename _Traits>
588 inline basic_ostream<_CharT, _Traits>&
589 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
592 __out.setstate(ios_base::badbit);
594 __ostream_insert(__out, __s,
595 static_cast<streamsize>(_Traits::length(__s)));
599 template<typename _CharT, typename _Traits>
600 basic_ostream<_CharT, _Traits> &
601 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
603 // Partial specializations
604 template<typename _Traits>
605 inline basic_ostream<char, _Traits>&
606 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
609 __out.setstate(ios_base::badbit);
611 __ostream_insert(__out, __s,
612 static_cast<streamsize>(_Traits::length(__s)));
616 // Signed and unsigned
617 template<typename _Traits>
618 inline basic_ostream<char, _Traits>&
619 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
620 { return (__out << reinterpret_cast<const char*>(__s)); }
622 template<typename _Traits>
623 inline basic_ostream<char, _Traits> &
624 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
625 { return (__out << reinterpret_cast<const char*>(__s)); }
627 #if __cplusplus > 201703L
628 // The following deleted overloads prevent formatting strings as
631 #ifdef _GLIBCXX_USE_WCHAR_T
632 template<typename _Traits>
633 basic_ostream<char, _Traits>&
634 operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
635 #endif // _GLIBCXX_USE_WCHAR_T
637 #ifdef _GLIBCXX_USE_CHAR8_T
638 template<typename _Traits>
639 basic_ostream<char, _Traits>&
640 operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
641 #endif // _GLIBCXX_USE_CHAR8_T
643 template<typename _Traits>
644 basic_ostream<char, _Traits>&
645 operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
647 template<typename _Traits>
648 basic_ostream<char, _Traits>&
649 operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
651 #ifdef _GLIBCXX_USE_WCHAR_T
652 #ifdef _GLIBCXX_USE_CHAR8_T
653 template<typename _Traits>
654 basic_ostream<wchar_t, _Traits>&
655 operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
658 template<typename _Traits>
659 basic_ostream<wchar_t, _Traits>&
660 operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
662 template<typename _Traits>
663 basic_ostream<wchar_t, _Traits>&
664 operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
665 #endif // _GLIBCXX_USE_WCHAR_T
669 // Standard basic_ostream manipulators
672 * @brief Write a newline and flush the stream.
674 * This manipulator is often mistakenly used when a simple newline is
675 * desired, leading to poor buffering performance. See
676 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
677 * for more on this subject.
679 template<typename _CharT, typename _Traits>
680 inline basic_ostream<_CharT, _Traits>&
681 endl(basic_ostream<_CharT, _Traits>& __os)
682 { return flush(__os.put(__os.widen('\n'))); }
685 * @brief Write a null character into the output sequence.
687 * <em>Null character</em> is @c CharT() by definition. For CharT
688 * of @c char, this correctly writes the ASCII @c NUL character
691 template<typename _CharT, typename _Traits>
692 inline basic_ostream<_CharT, _Traits>&
693 ends(basic_ostream<_CharT, _Traits>& __os)
694 { return __os.put(_CharT()); }
697 * @brief Flushes the output stream.
699 * This manipulator simply calls the stream's @c flush() member function.
701 template<typename _CharT, typename _Traits>
702 inline basic_ostream<_CharT, _Traits>&
703 flush(basic_ostream<_CharT, _Traits>& __os)
704 { return __os.flush(); }
706 #if __cplusplus >= 201103L
707 template<typename _Ch, typename _Up>
708 basic_ostream<_Ch, _Up>&
709 __is_convertible_to_basic_ostream_test(basic_ostream<_Ch, _Up>*);
711 template<typename _Tp, typename = void>
712 struct __is_convertible_to_basic_ostream_impl
714 using __ostream_type = void;
717 template<typename _Tp>
718 using __do_is_convertible_to_basic_ostream_impl =
719 decltype(__is_convertible_to_basic_ostream_test
720 (declval<typename remove_reference<_Tp>::type*>()));
722 template<typename _Tp>
723 struct __is_convertible_to_basic_ostream_impl
725 __void_t<__do_is_convertible_to_basic_ostream_impl<_Tp>>>
727 using __ostream_type =
728 __do_is_convertible_to_basic_ostream_impl<_Tp>;
731 template<typename _Tp>
732 struct __is_convertible_to_basic_ostream
733 : __is_convertible_to_basic_ostream_impl<_Tp>
736 using type = __not_<is_void<
737 typename __is_convertible_to_basic_ostream_impl<_Tp>::__ostream_type>>;
738 constexpr static bool value = type::value;
741 template<typename _Ostream, typename _Tp, typename = void>
742 struct __is_insertable : false_type {};
744 template<typename _Ostream, typename _Tp>
745 struct __is_insertable<_Ostream, _Tp,
746 __void_t<decltype(declval<_Ostream&>()
747 << declval<const _Tp&>())>>
750 template<typename _Ostream>
751 using __rvalue_ostream_type =
752 typename __is_convertible_to_basic_ostream<
753 _Ostream>::__ostream_type;
756 * @brief Generic inserter for rvalue stream
757 * @param __os An input stream.
758 * @param __x A reference to the object being inserted.
761 * This is just a forwarding function to allow insertion to
762 * rvalue streams since they won't bind to the inserter functions
763 * that take an lvalue reference.
765 template<typename _Ostream, typename _Tp>
767 typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,
768 __is_convertible_to_basic_ostream<_Ostream>,
770 __rvalue_ostream_type<_Ostream>,
772 __rvalue_ostream_type<_Ostream>>::type
773 operator<<(_Ostream&& __os, const _Tp& __x)
775 __rvalue_ostream_type<_Ostream> __ret_os = __os;
781 _GLIBCXX_END_NAMESPACE_VERSION
784 #include <bits/ostream.tcc>
786 #endif /* _GLIBCXX_OSTREAM */