You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

787 lines
24KB

  1. // Output streams -*- C++ -*-
  2. // Copyright (C) 1997-2020 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. /**
  221. * @brief Extracting from another streambuf.
  222. * @param __sb A pointer to a streambuf
  223. *
  224. * This function behaves like one of the basic arithmetic extractors,
  225. * in that it also constructs a sentry object and has the same error
  226. * handling behavior.
  227. *
  228. * If @p __sb is NULL, the stream will set failbit in its error state.
  229. *
  230. * Characters are extracted from @p __sb and inserted into @c *this
  231. * until one of the following occurs:
  232. *
  233. * - the input stream reaches end-of-file,
  234. * - insertion into the output sequence fails (in this case, the
  235. * character that would have been inserted is not extracted), or
  236. * - an exception occurs while getting a character from @p __sb, which
  237. * sets failbit in the error state
  238. *
  239. * If the function inserts no characters, failbit is set.
  240. */
  241. __ostream_type&
  242. operator<<(__streambuf_type* __sb);
  243. //@}
  244. //@{
  245. /**
  246. * @name Unformatted Output Functions
  247. *
  248. * All the unformatted output functions have some common behavior.
  249. * Each starts by constructing a temporary object of type
  250. * std::basic_ostream::sentry. This has several effects, concluding
  251. * with the setting of a status flag; see the sentry documentation
  252. * for more.
  253. *
  254. * If the sentry status is good, the function tries to generate
  255. * whatever data is appropriate for the type of the argument.
  256. *
  257. * If an exception is thrown during insertion, ios_base::badbit
  258. * will be turned on in the stream's error state. If badbit is on in
  259. * the stream's exceptions mask, the exception will be rethrown
  260. * without completing its actions.
  261. */
  262. /**
  263. * @brief Simple insertion.
  264. * @param __c The character to insert.
  265. * @return *this
  266. *
  267. * Tries to insert @p __c.
  268. *
  269. * @note This function is not overloaded on signed char and
  270. * unsigned char.
  271. */
  272. __ostream_type&
  273. put(char_type __c);
  274. /**
  275. * @brief Core write functionality, without sentry.
  276. * @param __s The array to insert.
  277. * @param __n Maximum number of characters to insert.
  278. */
  279. void
  280. _M_write(const char_type* __s, streamsize __n)
  281. {
  282. const streamsize __put = this->rdbuf()->sputn(__s, __n);
  283. if (__put != __n)
  284. this->setstate(ios_base::badbit);
  285. }
  286. /**
  287. * @brief Character string insertion.
  288. * @param __s The array to insert.
  289. * @param __n Maximum number of characters to insert.
  290. * @return *this
  291. *
  292. * Characters are copied from @p __s and inserted into the stream until
  293. * one of the following happens:
  294. *
  295. * - @p __n characters are inserted
  296. * - inserting into the output sequence fails (in this case, badbit
  297. * will be set in the stream's error state)
  298. *
  299. * @note This function is not overloaded on signed char and
  300. * unsigned char.
  301. */
  302. __ostream_type&
  303. write(const char_type* __s, streamsize __n);
  304. //@}
  305. /**
  306. * @brief Synchronizing the stream buffer.
  307. * @return *this
  308. *
  309. * If @c rdbuf() is a null pointer, changes nothing.
  310. *
  311. * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
  312. * sets badbit.
  313. */
  314. __ostream_type&
  315. flush();
  316. /**
  317. * @brief Getting the current write position.
  318. * @return A file position object.
  319. *
  320. * If @c fail() is not false, returns @c pos_type(-1) to indicate
  321. * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
  322. */
  323. pos_type
  324. tellp();
  325. /**
  326. * @brief Changing the current write position.
  327. * @param __pos A file position object.
  328. * @return *this
  329. *
  330. * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
  331. * that function fails, sets failbit.
  332. */
  333. __ostream_type&
  334. seekp(pos_type);
  335. /**
  336. * @brief Changing the current write position.
  337. * @param __off A file offset object.
  338. * @param __dir The direction in which to seek.
  339. * @return *this
  340. *
  341. * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
  342. * If that function fails, sets failbit.
  343. */
  344. __ostream_type&
  345. seekp(off_type, ios_base::seekdir);
  346. protected:
  347. basic_ostream()
  348. { this->init(0); }
  349. #if __cplusplus >= 201103L
  350. // Non-standard constructor that does not call init()
  351. basic_ostream(basic_iostream<_CharT, _Traits>&) { }
  352. basic_ostream(const basic_ostream&) = delete;
  353. basic_ostream(basic_ostream&& __rhs)
  354. : __ios_type()
  355. { __ios_type::move(__rhs); }
  356. // 27.7.3.3 Assign/swap
  357. basic_ostream& operator=(const basic_ostream&) = delete;
  358. basic_ostream&
  359. operator=(basic_ostream&& __rhs)
  360. {
  361. swap(__rhs);
  362. return *this;
  363. }
  364. void
  365. swap(basic_ostream& __rhs)
  366. { __ios_type::swap(__rhs); }
  367. #endif
  368. template<typename _ValueT>
  369. __ostream_type&
  370. _M_insert(_ValueT __v);
  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. { return __ostream_insert(__out, &__c, 1); }
  452. template<typename _CharT, typename _Traits>
  453. inline basic_ostream<_CharT, _Traits>&
  454. operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
  455. { return (__out << __out.widen(__c)); }
  456. // Specialization
  457. template<typename _Traits>
  458. inline basic_ostream<char, _Traits>&
  459. operator<<(basic_ostream<char, _Traits>& __out, char __c)
  460. { return __ostream_insert(__out, &__c, 1); }
  461. // Signed and unsigned
  462. template<typename _Traits>
  463. inline basic_ostream<char, _Traits>&
  464. operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
  465. { return (__out << static_cast<char>(__c)); }
  466. template<typename _Traits>
  467. inline basic_ostream<char, _Traits>&
  468. operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
  469. { return (__out << static_cast<char>(__c)); }
  470. #if __cplusplus > 201703L
  471. // The following deleted overloads prevent formatting character values as
  472. // numeric values.
  473. #ifdef _GLIBCXX_USE_WCHAR_T
  474. template<typename _Traits>
  475. basic_ostream<char, _Traits>&
  476. operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
  477. #endif // _GLIBCXX_USE_WCHAR_T
  478. #ifdef _GLIBCXX_USE_CHAR8_T
  479. template<typename _Traits>
  480. basic_ostream<char, _Traits>&
  481. operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
  482. #endif
  483. template<typename _Traits>
  484. basic_ostream<char, _Traits>&
  485. operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
  486. template<typename _Traits>
  487. basic_ostream<char, _Traits>&
  488. operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
  489. #ifdef _GLIBCXX_USE_WCHAR_T
  490. #ifdef _GLIBCXX_USE_CHAR8_T
  491. template<typename _Traits>
  492. basic_ostream<wchar_t, _Traits>&
  493. operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
  494. #endif // _GLIBCXX_USE_CHAR8_T
  495. template<typename _Traits>
  496. basic_ostream<wchar_t, _Traits>&
  497. operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
  498. template<typename _Traits>
  499. basic_ostream<wchar_t, _Traits>&
  500. operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
  501. #endif // _GLIBCXX_USE_WCHAR_T
  502. #endif // C++20
  503. //@}
  504. //@{
  505. /**
  506. * @brief String inserters
  507. * @param __out An output stream.
  508. * @param __s A character string.
  509. * @return out
  510. * @pre @p __s must be a non-NULL pointer
  511. *
  512. * Behaves like one of the formatted arithmetic inserters described in
  513. * std::basic_ostream. After constructing a sentry object with good
  514. * status, this function inserts @c traits::length(__s) characters starting
  515. * at @p __s, widened if necessary, followed by any required padding (as
  516. * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
  517. */
  518. template<typename _CharT, typename _Traits>
  519. inline basic_ostream<_CharT, _Traits>&
  520. operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
  521. {
  522. if (!__s)
  523. __out.setstate(ios_base::badbit);
  524. else
  525. __ostream_insert(__out, __s,
  526. static_cast<streamsize>(_Traits::length(__s)));
  527. return __out;
  528. }
  529. template<typename _CharT, typename _Traits>
  530. basic_ostream<_CharT, _Traits> &
  531. operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
  532. // Partial specializations
  533. template<typename _Traits>
  534. inline basic_ostream<char, _Traits>&
  535. operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
  536. {
  537. if (!__s)
  538. __out.setstate(ios_base::badbit);
  539. else
  540. __ostream_insert(__out, __s,
  541. static_cast<streamsize>(_Traits::length(__s)));
  542. return __out;
  543. }
  544. // Signed and unsigned
  545. template<typename _Traits>
  546. inline basic_ostream<char, _Traits>&
  547. operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
  548. { return (__out << reinterpret_cast<const char*>(__s)); }
  549. template<typename _Traits>
  550. inline basic_ostream<char, _Traits> &
  551. operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
  552. { return (__out << reinterpret_cast<const char*>(__s)); }
  553. #if __cplusplus > 201703L
  554. // The following deleted overloads prevent formatting strings as
  555. // pointer values.
  556. #ifdef _GLIBCXX_USE_WCHAR_T
  557. template<typename _Traits>
  558. basic_ostream<char, _Traits>&
  559. operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
  560. #endif // _GLIBCXX_USE_WCHAR_T
  561. #ifdef _GLIBCXX_USE_CHAR8_T
  562. template<typename _Traits>
  563. basic_ostream<char, _Traits>&
  564. operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
  565. #endif // _GLIBCXX_USE_CHAR8_T
  566. template<typename _Traits>
  567. basic_ostream<char, _Traits>&
  568. operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
  569. template<typename _Traits>
  570. basic_ostream<char, _Traits>&
  571. operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
  572. #ifdef _GLIBCXX_USE_WCHAR_T
  573. #ifdef _GLIBCXX_USE_CHAR8_T
  574. template<typename _Traits>
  575. basic_ostream<wchar_t, _Traits>&
  576. operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
  577. #endif
  578. template<typename _Traits>
  579. basic_ostream<wchar_t, _Traits>&
  580. operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
  581. template<typename _Traits>
  582. basic_ostream<wchar_t, _Traits>&
  583. operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
  584. #endif // _GLIBCXX_USE_WCHAR_T
  585. #endif // C++20
  586. //@}
  587. // Standard basic_ostream manipulators
  588. /**
  589. * @brief Write a newline and flush the stream.
  590. *
  591. * This manipulator is often mistakenly used when a simple newline is
  592. * desired, leading to poor buffering performance. See
  593. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  594. * for more on this subject.
  595. */
  596. template<typename _CharT, typename _Traits>
  597. inline basic_ostream<_CharT, _Traits>&
  598. endl(basic_ostream<_CharT, _Traits>& __os)
  599. { return flush(__os.put(__os.widen('\n'))); }
  600. /**
  601. * @brief Write a null character into the output sequence.
  602. *
  603. * <em>Null character</em> is @c CharT() by definition. For CharT
  604. * of @c char, this correctly writes the ASCII @c NUL character
  605. * string terminator.
  606. */
  607. template<typename _CharT, typename _Traits>
  608. inline basic_ostream<_CharT, _Traits>&
  609. ends(basic_ostream<_CharT, _Traits>& __os)
  610. { return __os.put(_CharT()); }
  611. /**
  612. * @brief Flushes the output stream.
  613. *
  614. * This manipulator simply calls the stream's @c flush() member function.
  615. */
  616. template<typename _CharT, typename _Traits>
  617. inline basic_ostream<_CharT, _Traits>&
  618. flush(basic_ostream<_CharT, _Traits>& __os)
  619. { return __os.flush(); }
  620. #if __cplusplus >= 201103L
  621. template<typename _Ch, typename _Up>
  622. basic_ostream<_Ch, _Up>&
  623. __is_convertible_to_basic_ostream_test(basic_ostream<_Ch, _Up>*);
  624. template<typename _Tp, typename = void>
  625. struct __is_convertible_to_basic_ostream_impl
  626. {
  627. using __ostream_type = void;
  628. };
  629. template<typename _Tp>
  630. using __do_is_convertible_to_basic_ostream_impl =
  631. decltype(__is_convertible_to_basic_ostream_test
  632. (declval<typename remove_reference<_Tp>::type*>()));
  633. template<typename _Tp>
  634. struct __is_convertible_to_basic_ostream_impl
  635. <_Tp,
  636. __void_t<__do_is_convertible_to_basic_ostream_impl<_Tp>>>
  637. {
  638. using __ostream_type =
  639. __do_is_convertible_to_basic_ostream_impl<_Tp>;
  640. };
  641. template<typename _Tp>
  642. struct __is_convertible_to_basic_ostream
  643. : __is_convertible_to_basic_ostream_impl<_Tp>
  644. {
  645. public:
  646. using type = __not_<is_void<
  647. typename __is_convertible_to_basic_ostream_impl<_Tp>::__ostream_type>>;
  648. constexpr static bool value = type::value;
  649. };
  650. template<typename _Ostream, typename _Tp, typename = void>
  651. struct __is_insertable : false_type {};
  652. template<typename _Ostream, typename _Tp>
  653. struct __is_insertable<_Ostream, _Tp,
  654. __void_t<decltype(declval<_Ostream&>()
  655. << declval<const _Tp&>())>>
  656. : true_type {};
  657. template<typename _Ostream>
  658. using __rvalue_ostream_type =
  659. typename __is_convertible_to_basic_ostream<
  660. _Ostream>::__ostream_type;
  661. /**
  662. * @brief Generic inserter for rvalue stream
  663. * @param __os An input stream.
  664. * @param __x A reference to the object being inserted.
  665. * @return os
  666. *
  667. * This is just a forwarding function to allow insertion to
  668. * rvalue streams since they won't bind to the inserter functions
  669. * that take an lvalue reference.
  670. */
  671. template<typename _Ostream, typename _Tp>
  672. inline
  673. typename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,
  674. __is_convertible_to_basic_ostream<_Ostream>,
  675. __is_insertable<
  676. __rvalue_ostream_type<_Ostream>,
  677. const _Tp&>>::value,
  678. __rvalue_ostream_type<_Ostream>>::type
  679. operator<<(_Ostream&& __os, const _Tp& __x)
  680. {
  681. __rvalue_ostream_type<_Ostream> __ret_os = __os;
  682. __ret_os << __x;
  683. return __ret_os;
  684. }
  685. #endif // C++11
  686. _GLIBCXX_END_NAMESPACE_VERSION
  687. } // namespace std
  688. #include <bits/ostream.tcc>
  689. #endif /* _GLIBCXX_OSTREAM */