ostream 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // Output streams -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/ostream
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.6.2 Output streams
  25. //
  26. #ifndef _GLIBCXX_OSTREAM
  27. #define _GLIBCXX_OSTREAM 1
  28. #pragma GCC system_header
  29. #include <ios>
  30. #include <bits/ostream_insert.h>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /**
  35. * @brief Template class basic_ostream.
  36. * @ingroup io
  37. *
  38. * @tparam _CharT Type of character stream.
  39. * @tparam _Traits Traits for character type, defaults to
  40. * char_traits<_CharT>.
  41. *
  42. * This is the base class for all output streams. It provides text
  43. * formatting of all builtin types, and communicates with any class
  44. * derived from basic_streambuf to do the actual output.
  45. */
  46. template<typename _CharT, typename _Traits>
  47. class basic_ostream : virtual public basic_ios<_CharT, _Traits>
  48. {
  49. public:
  50. // Types (inherited from basic_ios):
  51. typedef _CharT char_type;
  52. typedef typename _Traits::int_type int_type;
  53. typedef typename _Traits::pos_type pos_type;
  54. typedef typename _Traits::off_type off_type;
  55. typedef _Traits traits_type;
  56. // Non-standard Types:
  57. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  58. typedef basic_ios<_CharT, _Traits> __ios_type;
  59. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  60. typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
  61. __num_put_type;
  62. typedef ctype<_CharT> __ctype_type;
  63. /**
  64. * @brief Base constructor.
  65. *
  66. * This ctor is almost never called by the user directly, rather from
  67. * derived classes' initialization lists, which pass a pointer to
  68. * their own stream buffer.
  69. */
  70. explicit
  71. basic_ostream(__streambuf_type* __sb)
  72. { this->init(__sb); }
  73. /**
  74. * @brief Base destructor.
  75. *
  76. * This does very little apart from providing a virtual base dtor.
  77. */
  78. virtual
  79. ~basic_ostream() { }
  80. /// Safe prefix/suffix operations.
  81. class sentry;
  82. friend class sentry;
  83. ///@{
  84. /**
  85. * @brief Interface for manipulators.
  86. *
  87. * Manipulators such as @c std::endl and @c std::hex use these
  88. * functions in constructs like "std::cout << std::endl". For more
  89. * information, see the iomanip header.
  90. */
  91. __ostream_type&
  92. operator<<(__ostream_type& (*__pf)(__ostream_type&))
  93. {
  94. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  95. // DR 60. What is a formatted input function?
  96. // The inserters for manipulators are *not* formatted output functions.
  97. return __pf(*this);
  98. }
  99. __ostream_type&
  100. operator<<(__ios_type& (*__pf)(__ios_type&))
  101. {
  102. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  103. // DR 60. What is a formatted input function?
  104. // The inserters for manipulators are *not* formatted output functions.
  105. __pf(*this);
  106. return *this;
  107. }
  108. __ostream_type&
  109. operator<<(ios_base& (*__pf) (ios_base&))
  110. {
  111. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  112. // DR 60. What is a formatted input function?
  113. // The inserters for manipulators are *not* formatted output functions.
  114. __pf(*this);
  115. return *this;
  116. }
  117. ///@}
  118. ///@{
  119. /**
  120. * @name Inserters
  121. *
  122. * All the @c operator<< functions (aka <em>formatted output
  123. * functions</em>) have some common behavior. Each starts by
  124. * constructing a temporary object of type std::basic_ostream::sentry.
  125. * This can have several effects, concluding with the setting of a
  126. * status flag; see the sentry documentation for more.
  127. *
  128. * If the sentry status is good, the function tries to generate
  129. * whatever data is appropriate for the type of the argument.
  130. *
  131. * If an exception is thrown during insertion, ios_base::badbit
  132. * will be turned on in the stream's error state without causing an
  133. * ios_base::failure to be thrown. The original exception will then
  134. * be rethrown.
  135. */
  136. ///@{
  137. /**
  138. * @brief Integer arithmetic inserters
  139. * @param __n A variable of builtin integral type.
  140. * @return @c *this if successful
  141. *
  142. * These functions use the stream's current locale (specifically, the
  143. * @c num_get facet) to perform numeric formatting.
  144. */
  145. __ostream_type&
  146. operator<<(long __n)
  147. { return _M_insert(__n); }
  148. __ostream_type&
  149. operator<<(unsigned long __n)
  150. { return _M_insert(__n); }
  151. __ostream_type&
  152. operator<<(bool __n)
  153. { return _M_insert(__n); }
  154. __ostream_type&
  155. operator<<(short __n);
  156. __ostream_type&
  157. operator<<(unsigned short __n)
  158. {
  159. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  160. // 117. basic_ostream uses nonexistent num_put member functions.
  161. return _M_insert(static_cast<unsigned long>(__n));
  162. }
  163. __ostream_type&
  164. operator<<(int __n);
  165. __ostream_type&
  166. operator<<(unsigned int __n)
  167. {
  168. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  169. // 117. basic_ostream uses nonexistent num_put member functions.
  170. return _M_insert(static_cast<unsigned long>(__n));
  171. }
  172. #ifdef _GLIBCXX_USE_LONG_LONG
  173. __ostream_type&
  174. operator<<(long long __n)
  175. { return _M_insert(__n); }
  176. __ostream_type&
  177. operator<<(unsigned long long __n)
  178. { return _M_insert(__n); }
  179. #endif
  180. ///@}
  181. ///@{
  182. /**
  183. * @brief Floating point arithmetic inserters
  184. * @param __f A variable of builtin floating point type.
  185. * @return @c *this if successful
  186. *
  187. * These functions use the stream's current locale (specifically, the
  188. * @c num_get facet) to perform numeric formatting.
  189. */
  190. __ostream_type&
  191. operator<<(double __f)
  192. { return _M_insert(__f); }
  193. __ostream_type&
  194. operator<<(float __f)
  195. {
  196. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  197. // 117. basic_ostream uses nonexistent num_put member functions.
  198. return _M_insert(static_cast<double>(__f));
  199. }
  200. __ostream_type&
  201. operator<<(long double __f)
  202. { return _M_insert(__f); }
  203. ///@}
  204. /**
  205. * @brief Pointer arithmetic inserters
  206. * @param __p A variable of pointer type.
  207. * @return @c *this if successful
  208. *
  209. * These functions use the stream's current locale (specifically, the
  210. * @c num_get facet) to perform numeric formatting.
  211. */
  212. __ostream_type&
  213. operator<<(const void* __p)
  214. { return _M_insert(__p); }
  215. #if __cplusplus >= 201703L
  216. __ostream_type&
  217. operator<<(nullptr_t)
  218. { return *this << "nullptr"; }
  219. #endif
  220. #if __cplusplus > 202002L
  221. __attribute__((__always_inline__))
  222. __ostream_type&
  223. operator<<(const volatile void* __p)
  224. { return _M_insert(const_cast<const void*>(__p)); }
  225. #endif
  226. /**
  227. * @brief Extracting from another streambuf.
  228. * @param __sb A pointer to a streambuf
  229. *
  230. * This function behaves like one of the basic arithmetic extractors,
  231. * in that it also constructs a sentry object and has the same error
  232. * handling behavior.
  233. *
  234. * If @p __sb is NULL, the stream will set failbit in its error state.
  235. *
  236. * Characters are extracted from @p __sb and inserted into @c *this
  237. * until one of the following occurs:
  238. *
  239. * - the input stream reaches end-of-file,
  240. * - insertion into the output sequence fails (in this case, the
  241. * character that would have been inserted is not extracted), or
  242. * - an exception occurs while getting a character from @p __sb, which
  243. * sets failbit in the error state
  244. *
  245. * If the function inserts no characters, failbit is set.
  246. */
  247. __ostream_type&
  248. operator<<(__streambuf_type* __sb);
  249. ///@}
  250. ///@{
  251. /**
  252. * @name Unformatted Output Functions
  253. *
  254. * All the unformatted output functions have some common behavior.
  255. * Each starts by constructing a temporary object of type
  256. * std::basic_ostream::sentry. This has several effects, concluding
  257. * with the setting of a status flag; see the sentry documentation
  258. * for more.
  259. *
  260. * If the sentry status is good, the function tries to generate
  261. * whatever data is appropriate for the type of the argument.
  262. *
  263. * If an exception is thrown during insertion, ios_base::badbit
  264. * will be turned on in the stream's error state. If badbit is on in
  265. * the stream's exceptions mask, the exception will be rethrown
  266. * without completing its actions.
  267. */
  268. /**
  269. * @brief Simple insertion.
  270. * @param __c The character to insert.
  271. * @return *this
  272. *
  273. * Tries to insert @p __c.
  274. *
  275. * @note This function is not overloaded on signed char and
  276. * unsigned char.
  277. */
  278. __ostream_type&
  279. put(char_type __c);
  280. /**
  281. * @brief Character string insertion.
  282. * @param __s The array to insert.
  283. * @param __n Maximum number of characters to insert.
  284. * @return *this
  285. *
  286. * Characters are copied from @p __s and inserted into the stream until
  287. * one of the following happens:
  288. *
  289. * - @p __n characters are inserted
  290. * - inserting into the output sequence fails (in this case, badbit
  291. * will be set in the stream's error state)
  292. *
  293. * @note This function is not overloaded on signed char and
  294. * unsigned char.
  295. */
  296. __ostream_type&
  297. write(const char_type* __s, streamsize __n);
  298. ///@}
  299. /**
  300. * @brief Synchronizing the stream buffer.
  301. * @return *this
  302. *
  303. * If @c rdbuf() is a null pointer, changes nothing.
  304. *
  305. * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
  306. * sets badbit.
  307. */
  308. __ostream_type&
  309. flush();
  310. /**
  311. * @brief Getting the current write position.
  312. * @return A file position object.
  313. *
  314. * If @c fail() is not false, returns @c pos_type(-1) to indicate
  315. * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
  316. */
  317. pos_type
  318. tellp();
  319. /**
  320. * @brief Changing the current write position.
  321. * @param __pos A file position object.
  322. * @return *this
  323. *
  324. * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
  325. * that function fails, sets failbit.
  326. */
  327. __ostream_type&
  328. seekp(pos_type);
  329. /**
  330. * @brief Changing the current write position.
  331. * @param __off A file offset object.
  332. * @param __dir The direction in which to seek.
  333. * @return *this
  334. *
  335. * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
  336. * If that function fails, sets failbit.
  337. */
  338. __ostream_type&
  339. seekp(off_type, ios_base::seekdir);
  340. protected:
  341. basic_ostream()
  342. { this->init(0); }
  343. #if __cplusplus >= 201103L
  344. // Non-standard constructor that does not call init()
  345. basic_ostream(basic_iostream<_CharT, _Traits>&) { }
  346. basic_ostream(const basic_ostream&) = delete;
  347. basic_ostream(basic_ostream&& __rhs)
  348. : __ios_type()
  349. { __ios_type::move(__rhs); }
  350. // 27.7.3.3 Assign/swap
  351. basic_ostream& operator=(const basic_ostream&) = delete;
  352. basic_ostream&
  353. operator=(basic_ostream&& __rhs)
  354. {
  355. swap(__rhs);
  356. return *this;
  357. }
  358. void
  359. swap(basic_ostream& __rhs)
  360. { __ios_type::swap(__rhs); }
  361. #endif
  362. template<typename _ValueT>
  363. __ostream_type&
  364. _M_insert(_ValueT __v);
  365. private:
  366. #if !_GLIBCXX_INLINE_VERSION
  367. void
  368. _M_write(const char_type* __s, streamsize __n)
  369. { std::__ostream_insert(*this, __s, __n); }
  370. #endif
  371. };
  372. /**
  373. * @brief Performs setup work for output streams.
  374. *
  375. * Objects of this class are created before all of the standard
  376. * inserters are run. It is responsible for <em>exception-safe prefix and
  377. * suffix operations</em>.
  378. */
  379. template <typename _CharT, typename _Traits>
  380. class basic_ostream<_CharT, _Traits>::sentry
  381. {
  382. // Data Members.
  383. bool _M_ok;
  384. basic_ostream<_CharT, _Traits>& _M_os;
  385. public:
  386. /**
  387. * @brief The constructor performs preparatory work.
  388. * @param __os The output stream to guard.
  389. *
  390. * If the stream state is good (@a __os.good() is true), then if the
  391. * stream is tied to another output stream, @c is.tie()->flush()
  392. * is called to synchronize the output sequences.
  393. *
  394. * If the stream state is still good, then the sentry state becomes
  395. * true (@a okay).
  396. */
  397. explicit
  398. sentry(basic_ostream<_CharT, _Traits>& __os);
  399. #pragma GCC diagnostic push
  400. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  401. /**
  402. * @brief Possibly flushes the stream.
  403. *
  404. * If @c ios_base::unitbuf is set in @c os.flags(), and
  405. * @c std::uncaught_exception() is true, the sentry destructor calls
  406. * @c flush() on the output stream.
  407. */
  408. ~sentry()
  409. {
  410. // XXX MT
  411. if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
  412. {
  413. // Can't call flush directly or else will get into recursive lock.
  414. if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
  415. _M_os.setstate(ios_base::badbit);
  416. }
  417. }
  418. #pragma GCC diagnostic pop
  419. /**
  420. * @brief Quick status checking.
  421. * @return The sentry state.
  422. *
  423. * For ease of use, sentries may be converted to booleans. The
  424. * return value is that of the sentry state (true == okay).
  425. */
  426. #if __cplusplus >= 201103L
  427. explicit
  428. #endif
  429. operator bool() const
  430. { return _M_ok; }
  431. };
  432. ///@{
  433. /**
  434. * @brief Character inserters
  435. * @param __out An output stream.
  436. * @param __c A character.
  437. * @return out
  438. *
  439. * Behaves like one of the formatted arithmetic inserters described in
  440. * std::basic_ostream. After constructing a sentry object with good
  441. * status, this function inserts a single character and any required
  442. * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
  443. * called.
  444. *
  445. * If @p __c is of type @c char and the character type of the stream is not
  446. * @c char, the character is widened before insertion.
  447. */
  448. template<typename _CharT, typename _Traits>
  449. inline basic_ostream<_CharT, _Traits>&
  450. operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
  451. {
  452. if (__out.width() != 0)
  453. return __ostream_insert(__out, &__c, 1);
  454. __out.put(__c);
  455. return __out;
  456. }
  457. template<typename _CharT, typename _Traits>
  458. inline basic_ostream<_CharT, _Traits>&
  459. operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
  460. { return (__out << __out.widen(__c)); }
  461. // Specialization
  462. template<typename _Traits>
  463. inline basic_ostream<char, _Traits>&
  464. operator<<(basic_ostream<char, _Traits>& __out, char __c)
  465. {
  466. if (__out.width() != 0)
  467. return __ostream_insert(__out, &__c, 1);
  468. __out.put(__c);
  469. return __out;
  470. }
  471. // Signed and unsigned
  472. template<typename _Traits>
  473. inline basic_ostream<char, _Traits>&
  474. operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
  475. { return (__out << static_cast<char>(__c)); }
  476. template<typename _Traits>
  477. inline basic_ostream<char, _Traits>&
  478. operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
  479. { return (__out << static_cast<char>(__c)); }
  480. #if __cplusplus > 201703L
  481. // The following deleted overloads prevent formatting character values as
  482. // numeric values.
  483. template<typename _Traits>
  484. basic_ostream<char, _Traits>&
  485. operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
  486. #ifdef _GLIBCXX_USE_CHAR8_T
  487. template<typename _Traits>
  488. basic_ostream<char, _Traits>&
  489. operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
  490. #endif
  491. template<typename _Traits>
  492. basic_ostream<char, _Traits>&
  493. operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
  494. template<typename _Traits>
  495. basic_ostream<char, _Traits>&
  496. operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
  497. #ifdef _GLIBCXX_USE_WCHAR_T
  498. #ifdef _GLIBCXX_USE_CHAR8_T
  499. template<typename _Traits>
  500. basic_ostream<wchar_t, _Traits>&
  501. operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
  502. #endif // _GLIBCXX_USE_CHAR8_T
  503. template<typename _Traits>
  504. basic_ostream<wchar_t, _Traits>&
  505. operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
  506. template<typename _Traits>
  507. basic_ostream<wchar_t, _Traits>&
  508. operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
  509. #endif // _GLIBCXX_USE_WCHAR_T
  510. #endif // C++20
  511. ///@}
  512. ///@{
  513. /**
  514. * @brief String inserters
  515. * @param __out An output stream.
  516. * @param __s A character string.
  517. * @return out
  518. * @pre @p __s must be a non-NULL pointer
  519. *
  520. * Behaves like one of the formatted arithmetic inserters described in
  521. * std::basic_ostream. After constructing a sentry object with good
  522. * status, this function inserts @c traits::length(__s) characters starting
  523. * at @p __s, widened if necessary, followed by any required padding (as
  524. * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
  525. */
  526. template<typename _CharT, typename _Traits>
  527. inline basic_ostream<_CharT, _Traits>&
  528. operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
  529. {
  530. if (!__s)
  531. __out.setstate(ios_base::badbit);
  532. else
  533. __ostream_insert(__out, __s,
  534. static_cast<streamsize>(_Traits::length(__s)));
  535. return __out;
  536. }
  537. template<typename _CharT, typename _Traits>
  538. basic_ostream<_CharT, _Traits> &
  539. operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
  540. // Partial specializations
  541. template<typename _Traits>
  542. inline basic_ostream<char, _Traits>&
  543. operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
  544. {
  545. if (!__s)
  546. __out.setstate(ios_base::badbit);
  547. else
  548. __ostream_insert(__out, __s,
  549. static_cast<streamsize>(_Traits::length(__s)));
  550. return __out;
  551. }
  552. // Signed and unsigned
  553. template<typename _Traits>
  554. inline basic_ostream<char, _Traits>&
  555. operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
  556. { return (__out << reinterpret_cast<const char*>(__s)); }
  557. template<typename _Traits>
  558. inline basic_ostream<char, _Traits> &
  559. operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
  560. { return (__out << reinterpret_cast<const char*>(__s)); }
  561. #if __cplusplus > 201703L
  562. // The following deleted overloads prevent formatting strings as
  563. // pointer values.
  564. template<typename _Traits>
  565. basic_ostream<char, _Traits>&
  566. operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
  567. #ifdef _GLIBCXX_USE_CHAR8_T
  568. template<typename _Traits>
  569. basic_ostream<char, _Traits>&
  570. operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
  571. #endif // _GLIBCXX_USE_CHAR8_T
  572. template<typename _Traits>
  573. basic_ostream<char, _Traits>&
  574. operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
  575. template<typename _Traits>
  576. basic_ostream<char, _Traits>&
  577. operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
  578. #ifdef _GLIBCXX_USE_WCHAR_T
  579. #ifdef _GLIBCXX_USE_CHAR8_T
  580. template<typename _Traits>
  581. basic_ostream<wchar_t, _Traits>&
  582. operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
  583. #endif
  584. template<typename _Traits>
  585. basic_ostream<wchar_t, _Traits>&
  586. operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
  587. template<typename _Traits>
  588. basic_ostream<wchar_t, _Traits>&
  589. operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
  590. #endif // _GLIBCXX_USE_WCHAR_T
  591. #endif // C++20
  592. ///@}
  593. // Standard basic_ostream manipulators
  594. /**
  595. * @brief Write a newline and flush the stream.
  596. *
  597. * This manipulator is often mistakenly used when a simple newline is
  598. * desired, leading to poor buffering performance. See
  599. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  600. * for more on this subject.
  601. */
  602. template<typename _CharT, typename _Traits>
  603. inline basic_ostream<_CharT, _Traits>&
  604. endl(basic_ostream<_CharT, _Traits>& __os)
  605. { return flush(__os.put(__os.widen('\n'))); }
  606. /**
  607. * @brief Write a null character into the output sequence.
  608. *
  609. * <em>Null character</em> is @c CharT() by definition. For CharT
  610. * of @c char, this correctly writes the ASCII @c NUL character
  611. * string terminator.
  612. */
  613. template<typename _CharT, typename _Traits>
  614. inline basic_ostream<_CharT, _Traits>&
  615. ends(basic_ostream<_CharT, _Traits>& __os)
  616. { return __os.put(_CharT()); }
  617. /**
  618. * @brief Flushes the output stream.
  619. *
  620. * This manipulator simply calls the stream's @c flush() member function.
  621. */
  622. template<typename _CharT, typename _Traits>
  623. inline basic_ostream<_CharT, _Traits>&
  624. flush(basic_ostream<_CharT, _Traits>& __os)
  625. { return __os.flush(); }
  626. #if __cplusplus >= 201103L
  627. // C++11 27.7.3.9 Rvalue stream insertion [ostream.rvalue]
  628. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  629. // 1203. More useful rvalue stream insertion
  630. #if __cpp_lib_concepts
  631. // Use concepts if possible because they're cheaper to evaluate.
  632. template<typename _Tp>
  633. concept __derived_from_ios_base = is_class_v<_Tp>
  634. && (!is_same_v<_Tp, ios_base>)
  635. && requires (_Tp* __t, ios_base* __b) { __b = __t; };
  636. template<typename _Os, typename _Tp>
  637. requires __derived_from_ios_base<_Os>
  638. && requires (_Os& __os, const _Tp& __t) { __os << __t; }
  639. using __rvalue_stream_insertion_t = _Os&&;
  640. #else
  641. template<typename _Tp>
  642. using _Require_derived_from_ios_base
  643. = _Require<is_class<_Tp>, __not_<is_same<_Tp, ios_base>>,
  644. is_convertible<typename add_pointer<_Tp>::type, ios_base*>>;
  645. template<typename _Os, typename _Tp,
  646. typename = _Require_derived_from_ios_base<_Os>,
  647. typename
  648. = decltype(std::declval<_Os&>() << std::declval<const _Tp&>())>
  649. using __rvalue_stream_insertion_t = _Os&&;
  650. #endif
  651. /**
  652. * @brief Generic inserter for rvalue stream
  653. * @param __os An input stream.
  654. * @param __x A reference to the object being inserted.
  655. * @return __os
  656. *
  657. * This is just a forwarding function to allow insertion to
  658. * rvalue streams since they won't bind to the inserter functions
  659. * that take an lvalue reference.
  660. */
  661. template<typename _Ostream, typename _Tp>
  662. inline __rvalue_stream_insertion_t<_Ostream, _Tp>
  663. operator<<(_Ostream&& __os, const _Tp& __x)
  664. {
  665. __os << __x;
  666. return std::move(__os);
  667. }
  668. #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
  669. template<typename _CharT, typename _Traits>
  670. class __syncbuf_base : public basic_streambuf<_CharT, _Traits>
  671. {
  672. public:
  673. static bool*
  674. _S_get(basic_streambuf<_CharT, _Traits>* __buf [[maybe_unused]]) noexcept
  675. {
  676. #if __cpp_rtti
  677. if (auto __p = dynamic_cast<__syncbuf_base*>(__buf))
  678. return &__p->_M_emit_on_sync;
  679. #endif
  680. return nullptr;
  681. }
  682. protected:
  683. __syncbuf_base(basic_streambuf<_CharT, _Traits>* __w = nullptr)
  684. : _M_wrapped(__w)
  685. { }
  686. basic_streambuf<_CharT, _Traits>* _M_wrapped = nullptr;
  687. bool _M_emit_on_sync = false;
  688. bool _M_needs_sync = false;
  689. };
  690. template<typename _CharT, typename _Traits>
  691. inline basic_ostream<_CharT, _Traits>&
  692. emit_on_flush(basic_ostream<_CharT, _Traits>& __os)
  693. {
  694. if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
  695. *__flag = true;
  696. return __os;
  697. }
  698. template<typename _CharT, typename _Traits>
  699. inline basic_ostream<_CharT, _Traits>&
  700. noemit_on_flush(basic_ostream<_CharT, _Traits>& __os)
  701. {
  702. if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
  703. *__flag = false;
  704. return __os;
  705. }
  706. template<typename _CharT, typename _Traits>
  707. inline basic_ostream<_CharT, _Traits>&
  708. flush_emit(basic_ostream<_CharT, _Traits>& __os)
  709. {
  710. struct _Restore
  711. {
  712. ~_Restore() { *_M_flag = _M_prev; }
  713. bool _M_prev = false;
  714. bool* _M_flag = &_M_prev;
  715. } __restore;
  716. if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
  717. {
  718. __restore._M_prev = *__flag;
  719. __restore._M_flag = __flag;
  720. *__flag = true;
  721. }
  722. __os.flush();
  723. return __os;
  724. }
  725. #endif // C++20
  726. #endif // C++11
  727. _GLIBCXX_END_NAMESPACE_VERSION
  728. } // namespace std
  729. #include <bits/ostream.tcc>
  730. #endif /* _GLIBCXX_OSTREAM */