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