1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997-2017 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); }
249 * @brief Extracting from another streambuf.
250 * @param __sb A pointer to a streambuf
252 * This function behaves like one of the basic arithmetic extractors,
253 * in that it also constructs a sentry object and has the same error
256 * If @p __sb is NULL, the stream will set failbit in its error state.
258 * Characters are extracted from @p __sb and inserted into @c *this
259 * until one of the following occurs:
261 * - the input stream reaches end-of-file,
262 * - insertion into the output sequence fails (in this case, the
263 * character that would have been inserted is not extracted), or
264 * - an exception occurs while getting a character from @p __sb, which
265 * sets failbit in the error state
267 * If the function inserts no characters, failbit is set.
270 operator<<(__streambuf_type* __sb);
275 * @name Unformatted Output Functions
277 * All the unformatted output functions have some common behavior.
278 * Each starts by constructing a temporary object of type
279 * std::basic_ostream::sentry. This has several effects, concluding
280 * with the setting of a status flag; see the sentry documentation
283 * If the sentry status is good, the function tries to generate
284 * whatever data is appropriate for the type of the argument.
286 * If an exception is thrown during insertion, ios_base::badbit
287 * will be turned on in the stream's error state. If badbit is on in
288 * the stream's exceptions mask, the exception will be rethrown
289 * without completing its actions.
293 * @brief Simple insertion.
294 * @param __c The character to insert.
297 * Tries to insert @p __c.
299 * @note This function is not overloaded on signed char and
306 * @brief Core write functionality, without sentry.
307 * @param __s The array to insert.
308 * @param __n Maximum number of characters to insert.
311 _M_write(const char_type* __s, streamsize __n)
313 const streamsize __put = this->rdbuf()->sputn(__s, __n);
315 this->setstate(ios_base::badbit);
319 * @brief Character string insertion.
320 * @param __s The array to insert.
321 * @param __n Maximum number of characters to insert.
324 * Characters are copied from @p __s and inserted into the stream until
325 * one of the following happens:
327 * - @p __n characters are inserted
328 * - inserting into the output sequence fails (in this case, badbit
329 * will be set in the stream's error state)
331 * @note This function is not overloaded on signed char and
335 write(const char_type* __s, streamsize __n);
339 * @brief Synchronizing the stream buffer.
342 * If @c rdbuf() is a null pointer, changes nothing.
344 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
351 * @brief Getting the current write position.
352 * @return A file position object.
354 * If @c fail() is not false, returns @c pos_type(-1) to indicate
355 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
361 * @brief Changing the current write position.
362 * @param __pos A file position object.
365 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
366 * that function fails, sets failbit.
372 * @brief Changing the current write position.
373 * @param __off A file offset object.
374 * @param __dir The direction in which to seek.
377 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
378 * If that function fails, sets failbit.
381 seekp(off_type, ios_base::seekdir);
387 #if __cplusplus >= 201103L
388 // Non-standard constructor that does not call init()
389 basic_ostream(basic_iostream<_CharT, _Traits>&) { }
391 basic_ostream(const basic_ostream&) = delete;
393 basic_ostream(basic_ostream&& __rhs)
395 { __ios_type::move(__rhs); }
397 // 27.7.3.3 Assign/swap
399 basic_ostream& operator=(const basic_ostream&) = delete;
402 operator=(basic_ostream&& __rhs)
409 swap(basic_ostream& __rhs)
410 { __ios_type::swap(__rhs); }
413 template<typename _ValueT>
415 _M_insert(_ValueT __v);
419 * @brief Performs setup work for output streams.
421 * Objects of this class are created before all of the standard
422 * inserters are run. It is responsible for <em>exception-safe prefix and
423 * suffix operations</em>.
425 template <typename _CharT, typename _Traits>
426 class basic_ostream<_CharT, _Traits>::sentry
430 basic_ostream<_CharT, _Traits>& _M_os;
434 * @brief The constructor performs preparatory work.
435 * @param __os The output stream to guard.
437 * If the stream state is good (@a __os.good() is true), then if the
438 * stream is tied to another output stream, @c is.tie()->flush()
439 * is called to synchronize the output sequences.
441 * If the stream state is still good, then the sentry state becomes
445 sentry(basic_ostream<_CharT, _Traits>& __os);
448 * @brief Possibly flushes the stream.
450 * If @c ios_base::unitbuf is set in @c os.flags(), and
451 * @c std::uncaught_exception() is true, the sentry destructor calls
452 * @c flush() on the output stream.
457 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
459 // Can't call flush directly or else will get into recursive lock.
460 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
461 _M_os.setstate(ios_base::badbit);
466 * @brief Quick status checking.
467 * @return The sentry state.
469 * For ease of use, sentries may be converted to booleans. The
470 * return value is that of the sentry state (true == okay).
472 #if __cplusplus >= 201103L
475 operator bool() const
481 * @brief Character inserters
482 * @param __out An output stream.
483 * @param __c A character.
486 * Behaves like one of the formatted arithmetic inserters described in
487 * std::basic_ostream. After constructing a sentry object with good
488 * status, this function inserts a single character and any required
489 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
492 * If @p __c is of type @c char and the character type of the stream is not
493 * @c char, the character is widened before insertion.
495 template<typename _CharT, typename _Traits>
496 inline basic_ostream<_CharT, _Traits>&
497 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
498 { return __ostream_insert(__out, &__c, 1); }
500 template<typename _CharT, typename _Traits>
501 inline basic_ostream<_CharT, _Traits>&
502 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
503 { return (__out << __out.widen(__c)); }
506 template <class _Traits>
507 inline basic_ostream<char, _Traits>&
508 operator<<(basic_ostream<char, _Traits>& __out, char __c)
509 { return __ostream_insert(__out, &__c, 1); }
511 // Signed and unsigned
512 template<class _Traits>
513 inline basic_ostream<char, _Traits>&
514 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
515 { return (__out << static_cast<char>(__c)); }
517 template<class _Traits>
518 inline basic_ostream<char, _Traits>&
519 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
520 { return (__out << static_cast<char>(__c)); }
525 * @brief String inserters
526 * @param __out An output stream.
527 * @param __s A character string.
529 * @pre @p __s must be a non-NULL pointer
531 * Behaves like one of the formatted arithmetic inserters described in
532 * std::basic_ostream. After constructing a sentry object with good
533 * status, this function inserts @c traits::length(__s) characters starting
534 * at @p __s, widened if necessary, followed by any required padding (as
535 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
537 template<typename _CharT, typename _Traits>
538 inline basic_ostream<_CharT, _Traits>&
539 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
542 __out.setstate(ios_base::badbit);
544 __ostream_insert(__out, __s,
545 static_cast<streamsize>(_Traits::length(__s)));
549 template<typename _CharT, typename _Traits>
550 basic_ostream<_CharT, _Traits> &
551 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
553 // Partial specializations
554 template<class _Traits>
555 inline basic_ostream<char, _Traits>&
556 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
559 __out.setstate(ios_base::badbit);
561 __ostream_insert(__out, __s,
562 static_cast<streamsize>(_Traits::length(__s)));
566 // Signed and unsigned
567 template<class _Traits>
568 inline basic_ostream<char, _Traits>&
569 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
570 { return (__out << reinterpret_cast<const char*>(__s)); }
572 template<class _Traits>
573 inline basic_ostream<char, _Traits> &
574 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
575 { return (__out << reinterpret_cast<const char*>(__s)); }
578 // Standard basic_ostream manipulators
581 * @brief Write a newline and flush the stream.
583 * This manipulator is often mistakenly used when a simple newline is
584 * desired, leading to poor buffering performance. See
585 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
586 * for more on this subject.
588 template<typename _CharT, typename _Traits>
589 inline basic_ostream<_CharT, _Traits>&
590 endl(basic_ostream<_CharT, _Traits>& __os)
591 { return flush(__os.put(__os.widen('\n'))); }
594 * @brief Write a null character into the output sequence.
596 * <em>Null character</em> is @c CharT() by definition. For CharT
597 * of @c char, this correctly writes the ASCII @c NUL character
600 template<typename _CharT, typename _Traits>
601 inline basic_ostream<_CharT, _Traits>&
602 ends(basic_ostream<_CharT, _Traits>& __os)
603 { return __os.put(_CharT()); }
606 * @brief Flushes the output stream.
608 * This manipulator simply calls the stream's @c flush() member function.
610 template<typename _CharT, typename _Traits>
611 inline basic_ostream<_CharT, _Traits>&
612 flush(basic_ostream<_CharT, _Traits>& __os)
613 { return __os.flush(); }
615 #if __cplusplus >= 201103L
616 template<typename _Ch, typename _Up>
617 basic_ostream<_Ch, _Up>&
618 __is_convertible_to_basic_ostream_test(basic_ostream<_Ch, _Up>*);
620 template<typename _Tp, typename = void>
621 struct __is_convertible_to_basic_ostream_impl
623 using __ostream_type = void;
626 template<typename _Tp>
627 using __do_is_convertible_to_basic_ostream_impl =
628 decltype(__is_convertible_to_basic_ostream_test
629 (declval<typename remove_reference<_Tp>::type*>()));
631 template<typename _Tp>
632 struct __is_convertible_to_basic_ostream_impl
634 __void_t<__do_is_convertible_to_basic_ostream_impl<_Tp>>>
636 using __ostream_type =
637 __do_is_convertible_to_basic_ostream_impl<_Tp>;
640 template<typename _Tp>
641 struct __is_convertible_to_basic_ostream
642 : __is_convertible_to_basic_ostream_impl<_Tp>
645 using type = __not_<is_void<
646 typename __is_convertible_to_basic_ostream_impl<_Tp>::__ostream_type>>;
647 constexpr static bool value = type::value;
650 template<typename _Ostream, typename _Tp, typename = void>
651 struct __is_insertable : false_type {};
653 template<typename _Ostream, typename _Tp>
654 struct __is_insertable<_Ostream, _Tp,
655 __void_t<decltype(declval<_Ostream&>()
656 << declval<const _Tp&>())>>
659 template<typename _Ostream>
660 using __rvalue_ostream_type =
661 typename __is_convertible_to_basic_ostream<
662 _Ostream>::__ostream_type;
665 * @brief Generic inserter for rvalue stream
666 * @param __os An input stream.
667 * @param __x A reference to the object being inserted.
670 * This is just a forwarding function to allow insertion to
671 * rvalue streams since they won't bind to the inserter functions
672 * that take an lvalue reference.
674 template<typename _Ostream, typename _Tp>
676 typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,
677 __is_convertible_to_basic_ostream<_Ostream>,
679 __rvalue_ostream_type<_Ostream>,
681 __rvalue_ostream_type<_Ostream>>::type
682 operator<<(_Ostream&& __os, const _Tp& __x)
684 __rvalue_ostream_type<_Ostream> __ret_os = __os;
690 _GLIBCXX_END_NAMESPACE_VERSION
693 #include <bits/ostream.tcc>
695 #endif /* _GLIBCXX_OSTREAM */