Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/istream
1 // Input streams -*- C++ -*- 2 3 // Copyright (C) 1997-2025 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 // 26 // ISO C++ 14882: 27.6.1 Input streams 27 // 28 29 /** @file include/istream 30 * This is a Standard C++ Library header. 31 */ 32 33 #ifndef _GLIBCXX_ISTREAM 34 #define _GLIBCXX_ISTREAM 1 35 36 #ifdef _GLIBCXX_SYSHDR 37 #pragma GCC system_header 38 #endif 39 40 #include <bits/requires_hosted.h> // iostreams 41 42 #include <ios> 43 #include <ostream> 44 45 namespace std _GLIBCXX_VISIBILITY(default) 46 { 47 _GLIBCXX_BEGIN_NAMESPACE_VERSION 48 49 /** 50 * @brief Template class basic_istream. 51 * @ingroup io 52 * 53 * @tparam _CharT Type of character stream. 54 * @tparam _Traits Traits for character type, defaults to 55 * char_traits<_CharT>. 56 * 57 * This is the base class for all input streams. It provides text 58 * formatting of all builtin types, and communicates with any class 59 * derived from basic_streambuf to do the actual input. 60 */ 61 template<typename _CharT, typename _Traits> 62 class basic_istream : virtual public basic_ios<_CharT, _Traits> 63 { 64 public: 65 // Types (inherited from basic_ios (27.4.4)): 66 typedef _CharT char_type; 67 typedef typename _Traits::int_type int_type; 68 typedef typename _Traits::pos_type pos_type; 69 typedef typename _Traits::off_type off_type; 70 typedef _Traits traits_type; 71 72 // Non-standard Types: 73 typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 74 typedef basic_ios<_CharT, _Traits> __ios_type; 75 typedef basic_istream<_CharT, _Traits> __istream_type; 76 typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> > 77 __num_get_type; 78 typedef ctype<_CharT> __ctype_type; 79 80 protected: 81 // Data Members: 82 /** 83 * The number of characters extracted in the previous unformatted 84 * function; see gcount(). 85 */ 86 streamsize _M_gcount; 87 88 public: 89 /** 90 * @brief Base constructor. 91 * 92 * This ctor is almost never called by the user directly, rather from 93 * derived classes' initialization lists, which pass a pointer to 94 * their own stream buffer. 95 */ 96 explicit 97 basic_istream(__streambuf_type* __sb) 98 : _M_gcount(streamsize(0)) 99 { this->init(__sb); } 100 101 /** 102 * @brief Base destructor. 103 * 104 * This does very little apart from providing a virtual base dtor. 105 */ 106 virtual 107 ~basic_istream() 108 { _M_gcount = streamsize(0); } 109 110 /// Safe prefix/suffix operations. 111 class sentry; 112 friend class sentry; 113 114 ///@{ 115 /** 116 * @brief Interface for manipulators. 117 * 118 * Manipulators such as @c std::ws and @c std::dec use these 119 * functions in constructs like 120 * <code>std::cin >> std::ws</code>. 121 * For more information, see the iomanip header. 122 */ 123 __istream_type& 124 operator>>(__istream_type& (*__pf)(__istream_type&)) 125 { return __pf(*this); } 126 127 __istream_type& 128 operator>>(__ios_type& (*__pf)(__ios_type&)) 129 { 130 __pf(*this); 131 return *this; 132 } 133 134 __istream_type& 135 operator>>(ios_base& (*__pf)(ios_base&)) 136 { 137 __pf(*this); 138 return *this; 139 } 140 ///@} 141 142 ///@{ 143 /** 144 * @name Extractors 145 * 146 * All the @c operator>> functions (aka <em>formatted input 147 * functions</em>) have some common behavior. Each starts by 148 * constructing a temporary object of type std::basic_istream::sentry 149 * with the second argument (noskipws) set to false. This has several 150 * effects, concluding with the setting of a status flag; see the 151 * sentry documentation for more. 152 * 153 * If the sentry status is good, the function tries to extract 154 * whatever data is appropriate for the type of the argument. 155 * 156 * If an exception is thrown during extraction, ios_base::badbit 157 * will be turned on in the stream's error state (without causing an 158 * ios_base::failure to be thrown) and the original exception will 159 * be rethrown if badbit is set in the exceptions mask. 160 */ 161 162 ///@{ 163 /** 164 * @brief Integer arithmetic extractors 165 * @param __n A variable of builtin integral type. 166 * @return @c *this if successful 167 * 168 * These functions use the stream's current locale (specifically, the 169 * @c num_get facet) to parse the input data. 170 */ 171 __istream_type& 172 operator>>(bool& __n) 173 { return _M_extract(__n); } 174 175 __istream_type& 176 operator>>(short& __n); 177 178 __istream_type& 179 operator>>(unsigned short& __n) 180 { return _M_extract(__n); } 181 182 __istream_type& 183 operator>>(int& __n); 184 185 __istream_type& 186 operator>>(unsigned int& __n) 187 { return _M_extract(__n); } 188 189 __istream_type& 190 operator>>(long& __n) 191 { return _M_extract(__n); } 192 193 __istream_type& 194 operator>>(unsigned long& __n) 195 { return _M_extract(__n); } 196 197 #ifdef _GLIBCXX_USE_LONG_LONG 198 #pragma GCC diagnostic push 199 #pragma GCC diagnostic ignored "-Wlong-long" 200 __istream_type& 201 operator>>(long long& __n) 202 { return _M_extract(__n); } 203 204 __istream_type& 205 operator>>(unsigned long long& __n) 206 { return _M_extract(__n); } 207 #pragma GCC diagnostic pop 208 #endif 209 ///@} 210 211 ///@{ 212 /** 213 * @brief Floating point arithmetic extractors 214 * @param __f A variable of builtin floating point type. 215 * @return @c *this if successful 216 * 217 * These functions use the stream's current locale (specifically, the 218 * @c num_get facet) to parse the input data. 219 */ 220 __istream_type& 221 operator>>(float& __f) 222 { return _M_extract(__f); } 223 224 __istream_type& 225 operator>>(double& __f) 226 { return _M_extract(__f); } 227 228 __istream_type& 229 operator>>(long double& __f) 230 { return _M_extract(__f); } 231 ///@} 232 233 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) 234 __attribute__((__always_inline__)) 235 __istream_type& 236 operator>>(_Float16& __f) 237 { 238 float __flt; 239 __istream_type& __ret = _M_extract(__flt); 240 ios_base::iostate __err = ios_base::goodbit; 241 if (__flt < -__FLT16_MAX__) 242 { 243 __f = -__FLT16_MAX__; 244 __err = ios_base::failbit; 245 } 246 else if (__flt > __FLT16_MAX__) 247 { 248 __f = __FLT16_MAX__; 249 __err = ios_base::failbit; 250 } 251 else 252 __f = static_cast<_Float16>(__flt); 253 if (__err) 254 this->setstate(__err); 255 return __ret; 256 } 257 #endif 258 259 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) 260 __attribute__((__always_inline__)) 261 __istream_type& 262 operator>>(_Float32& __f) 263 { 264 float __flt; 265 __istream_type& __ret = _M_extract(__flt); 266 __f = static_cast<_Float32> (__flt); 267 return __ret; 268 } 269 #endif 270 271 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64) 272 __attribute__((__always_inline__)) 273 __istream_type& 274 operator>>(_Float64& __f) 275 { 276 double __dbl; 277 __istream_type& __ret = _M_extract(__dbl); 278 __f = static_cast<_Float64> (__dbl); 279 return __ret; 280 } 281 #endif 282 283 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) 284 __attribute__((__always_inline__)) 285 __istream_type& 286 operator>>(_Float128& __f) 287 { 288 long double __ldbl; 289 __istream_type& __ret = _M_extract(__ldbl); 290 __f = static_cast<_Float128> (__ldbl); 291 return __ret; 292 } 293 #endif 294 295 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) 296 __attribute__((__always_inline__)) 297 __istream_type& 298 operator>>(__gnu_cxx::__bfloat16_t & __f) 299 { 300 float __flt; 301 __istream_type& __ret = _M_extract(__flt); 302 ios_base::iostate __err = ios_base::goodbit; 303 if (__flt < -__BFLT16_MAX__) 304 { 305 __f = -__BFLT16_MAX__; 306 __err = ios_base::failbit; 307 } 308 else if (__flt > __BFLT16_MAX__) 309 { 310 __f = __BFLT16_MAX__; 311 __err = ios_base::failbit; 312 } 313 else 314 __f = static_cast<__gnu_cxx::__bfloat16_t>(__flt); 315 if (__err) 316 this->setstate(__err); 317 return __ret; 318 } 319 #endif 320 321 /** 322 * @brief Basic arithmetic extractors 323 * @param __p A variable of pointer type. 324 * @return @c *this if successful 325 * 326 * These functions use the stream's current locale (specifically, the 327 * @c num_get facet) to parse the input data. 328 */ 329 __istream_type& 330 operator>>(void*& __p) 331 { return _M_extract(__p); } 332 333 /** 334 * @brief Extracting into another streambuf. 335 * @param __sb A pointer to a streambuf 336 * 337 * This function behaves like one of the basic arithmetic extractors, 338 * in that it also constructs a sentry object and has the same error 339 * handling behavior. 340 * 341 * If @p __sb is NULL, the stream will set failbit in its error state. 342 * 343 * Characters are extracted from this stream and inserted into the 344 * @p __sb streambuf until one of the following occurs: 345 * 346 * - the input stream reaches end-of-file, 347 * - insertion into the output buffer fails (in this case, the 348 * character that would have been inserted is not extracted), or 349 * - an exception occurs (and in this case is caught) 350 * 351 * If the function inserts no characters, failbit is set. 352 */ 353 __istream_type& 354 operator>>(__streambuf_type* __sb); 355 ///@} 356 357 // [27.6.1.3] unformatted input 358 /** 359 * @brief Character counting 360 * @return The number of characters extracted by the previous 361 * unformatted input function dispatched for this stream. 362 */ 363 streamsize 364 gcount() const 365 { return _M_gcount; } 366 367 ///@{ 368 /** 369 * @name Unformatted Input Functions 370 * 371 * All the unformatted input functions have some common behavior. 372 * Each starts by constructing a temporary object of type 373 * std::basic_istream::sentry with the second argument (noskipws) 374 * set to true. This has several effects, concluding with the 375 * setting of a status flag; see the sentry documentation for more. 376 * 377 * If the sentry status is good, the function tries to extract 378 * whatever data is appropriate for the type of the argument. 379 * 380 * The number of characters extracted is stored for later retrieval 381 * by gcount(). 382 * 383 * If an exception is thrown during extraction, ios_base::badbit 384 * will be turned on in the stream's error state (without causing an 385 * ios_base::failure to be thrown) and the original exception will 386 * be rethrown if badbit is set in the exceptions mask. 387 */ 388 389 /** 390 * @brief Simple extraction. 391 * @return A character, or eof(). 392 * 393 * Tries to extract a character. If none are available, sets failbit 394 * and returns traits::eof(). 395 */ 396 int_type 397 get(); 398 399 /** 400 * @brief Simple extraction. 401 * @param __c The character in which to store data. 402 * @return *this 403 * 404 * Tries to extract a character and store it in @a __c. If none are 405 * available, sets failbit and returns traits::eof(). 406 * 407 * @note This function is not overloaded on signed char and 408 * unsigned char. 409 */ 410 __istream_type& 411 get(char_type& __c); 412 413 /** 414 * @brief Simple multiple-character extraction. 415 * @param __s Pointer to an array. 416 * @param __n Maximum number of characters to store in @a __s. 417 * @param __delim A "stop" character. 418 * @return *this 419 * 420 * Characters are extracted and stored into @a __s until one of the 421 * following happens: 422 * 423 * - @c __n-1 characters are stored 424 * - the input sequence reaches EOF 425 * - the next character equals @a __delim, in which case the character 426 * is not extracted 427 * 428 * If no characters are stored, failbit is set in the stream's error 429 * state. 430 * 431 * In any case, a null character is stored into the next location in 432 * the array. 433 * 434 * @note This function is not overloaded on signed char and 435 * unsigned char. 436 */ 437 __istream_type& 438 get(char_type* __s, streamsize __n, char_type __delim); 439 440 /** 441 * @brief Simple multiple-character extraction. 442 * @param __s Pointer to an array. 443 * @param __n Maximum number of characters to store in @a s. 444 * @return *this 445 * 446 * Returns @c get(__s,__n,widen('\\n')). 447 */ 448 __istream_type& 449 get(char_type* __s, streamsize __n) 450 { return this->get(__s, __n, this->widen('\n')); } 451 452 /** 453 * @brief Extraction into another streambuf. 454 * @param __sb A streambuf in which to store data. 455 * @param __delim A "stop" character. 456 * @return *this 457 * 458 * Characters are extracted and inserted into @a __sb until one of the 459 * following happens: 460 * 461 * - the input sequence reaches EOF 462 * - insertion into the output buffer fails (in this case, the 463 * character that would have been inserted is not extracted) 464 * - the next character equals @a __delim (in this case, the character 465 * is not extracted) 466 * - an exception occurs (and in this case is caught) 467 * 468 * If no characters are stored, failbit is set in the stream's error 469 * state. 470 */ 471 __istream_type& 472 get(__streambuf_type& __sb, char_type __delim); 473 474 /** 475 * @brief Extraction into another streambuf. 476 * @param __sb A streambuf in which to store data. 477 * @return *this 478 * 479 * Returns @c get(__sb,widen('\\n')). 480 */ 481 __istream_type& 482 get(__streambuf_type& __sb) 483 { return this->get(__sb, this->widen('\n')); } 484 485 /** 486 * @brief String extraction. 487 * @param __s A character array in which to store the data. 488 * @param __n Maximum number of characters to extract. 489 * @param __delim A "stop" character. 490 * @return *this 491 * 492 * Extracts and stores characters into @a __s until one of the 493 * following happens. Note that these criteria are required to be 494 * tested in the order listed here, to allow an input line to exactly 495 * fill the @a __s array without setting failbit. 496 * 497 * -# the input sequence reaches end-of-file, in which case eofbit 498 * is set in the stream error state 499 * -# the next character equals @c __delim, in which case the character 500 * is extracted (and therefore counted in @c gcount()) but not stored 501 * -# @c __n-1 characters are stored, in which case failbit is set 502 * in the stream error state 503 * 504 * If no characters are extracted, failbit is set. (An empty line of 505 * input should therefore not cause failbit to be set.) 506 * 507 * In any case, a null character is stored in the next location in 508 * the array. 509 */ 510 __istream_type& 511 getline(char_type* __s, streamsize __n, char_type __delim); 512 513 /** 514 * @brief String extraction. 515 * @param __s A character array in which to store the data. 516 * @param __n Maximum number of characters to extract. 517 * @return *this 518 * 519 * Returns @c getline(__s,__n,widen('\\n')). 520 */ 521 __istream_type& 522 getline(char_type* __s, streamsize __n) 523 { return this->getline(__s, __n, this->widen('\n')); } 524 525 /** 526 * @brief Discarding characters 527 * @param __n Number of characters to discard. 528 * @param __delim A "stop" character. 529 * @return *this 530 * 531 * Extracts characters and throws them away until one of the 532 * following happens: 533 * - if @a __n @c != @c std::numeric_limits<int>::max(), @a __n 534 * characters are extracted 535 * - the input sequence reaches end-of-file 536 * - the next character equals @a __delim (in this case, the character 537 * is extracted); note that this condition will never occur if 538 * @a __delim equals @c traits::eof(). 539 * 540 * NB: Provide three overloads, instead of the single function 541 * (with defaults) mandated by the Standard: this leads to a 542 * better performing implementation, while still conforming to 543 * the Standard. 544 */ 545 __istream_type& 546 ignore(streamsize __n, int_type __delim); 547 548 __istream_type& 549 ignore(streamsize __n); 550 551 __istream_type& 552 ignore(); 553 554 /** 555 * @brief Looking ahead in the stream 556 * @return The next character, or eof(). 557 * 558 * If, after constructing the sentry object, @c good() is false, 559 * returns @c traits::eof(). Otherwise reads but does not extract 560 * the next input character. 561 */ 562 int_type 563 peek(); 564 565 /** 566 * @brief Extraction without delimiters. 567 * @param __s A character array. 568 * @param __n Maximum number of characters to store. 569 * @return *this 570 * 571 * If the stream state is @c good(), extracts characters and stores 572 * them into @a __s until one of the following happens: 573 * - @a __n characters are stored 574 * - the input sequence reaches end-of-file, in which case the error 575 * state is set to @c failbit|eofbit. 576 * 577 * @note This function is not overloaded on signed char and 578 * unsigned char. 579 */ 580 __istream_type& 581 read(char_type* __s, streamsize __n); 582 583 /** 584 * @brief Extraction until the buffer is exhausted, but no more. 585 * @param __s A character array. 586 * @param __n Maximum number of characters to store. 587 * @return The number of characters extracted. 588 * 589 * Extracts characters and stores them into @a __s depending on the 590 * number of characters remaining in the streambuf's buffer, 591 * @c rdbuf()->in_avail(), called @c A here: 592 * - if @c A @c == @c -1, sets eofbit and extracts no characters 593 * - if @c A @c == @c 0, extracts no characters 594 * - if @c A @c > @c 0, extracts @c min(A,n) 595 * 596 * The goal is to empty the current buffer, and to not request any 597 * more from the external input sequence controlled by the streambuf. 598 */ 599 streamsize 600 readsome(char_type* __s, streamsize __n); 601 602 /** 603 * @brief Unextracting a single character. 604 * @param __c The character to push back into the input stream. 605 * @return *this 606 * 607 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c). 608 * 609 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in 610 * the error state. 611 * 612 * @note This function first clears eofbit. Since no characters 613 * are extracted, the next call to @c gcount() will return 0, 614 * as required by DR 60. 615 */ 616 __istream_type& 617 putback(char_type __c); 618 619 /** 620 * @brief Unextracting the previous character. 621 * @return *this 622 * 623 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c). 624 * 625 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in 626 * the error state. 627 * 628 * @note This function first clears eofbit. Since no characters 629 * are extracted, the next call to @c gcount() will return 0, 630 * as required by DR 60. 631 */ 632 __istream_type& 633 unget(); 634 635 /** 636 * @brief Synchronizing the stream buffer. 637 * @return 0 on success, -1 on failure 638 * 639 * If @c rdbuf() is a null pointer, returns -1. 640 * 641 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1, 642 * sets badbit and returns -1. 643 * 644 * Otherwise, returns 0. 645 * 646 * @note This function does not count the number of characters 647 * extracted, if any, and therefore does not affect the next 648 * call to @c gcount(). 649 */ 650 int 651 sync(); 652 653 /** 654 * @brief Getting the current read position. 655 * @return A file position object. 656 * 657 * If @c fail() is not false, returns @c pos_type(-1) to indicate 658 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in). 659 * 660 * @note This function does not count the number of characters 661 * extracted, if any, and therefore does not affect the next 662 * call to @c gcount(). At variance with putback, unget and 663 * seekg, eofbit is not cleared first. 664 */ 665 pos_type 666 tellg(); 667 668 /** 669 * @brief Changing the current read position. 670 * @param __pos A file position object. 671 * @return *this 672 * 673 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(__pos). If 674 * that function fails, sets failbit. 675 * 676 * @note This function first clears eofbit. It does not count the 677 * number of characters extracted, if any, and therefore does 678 * not affect the next call to @c gcount(). 679 */ 680 __istream_type& 681 seekg(pos_type); 682 683 /** 684 * @brief Changing the current read position. 685 * @param __off A file offset object. 686 * @param __dir The direction in which to seek. 687 * @return *this 688 * 689 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(__off,__dir). 690 * If that function fails, sets failbit. 691 * 692 * @note This function first clears eofbit. It does not count the 693 * number of characters extracted, if any, and therefore does 694 * not affect the next call to @c gcount(). 695 */ 696 __istream_type& 697 seekg(off_type, ios_base::seekdir); 698 ///@} 699 700 protected: 701 basic_istream() 702 : _M_gcount(streamsize(0)) 703 { this->init(0); } 704 705 #if __cplusplus >= 201103L 706 basic_istream(const basic_istream&) = delete; 707 708 basic_istream(basic_istream&& __rhs) 709 : __ios_type(), _M_gcount(__rhs._M_gcount) 710 { 711 __ios_type::move(__rhs); 712 __rhs._M_gcount = 0; 713 } 714 715 // 27.7.3.3 Assign/swap 716 717 basic_istream& operator=(const basic_istream&) = delete; 718 719 basic_istream& 720 operator=(basic_istream&& __rhs) 721 { 722 swap(__rhs); 723 return *this; 724 } 725 726 void 727 swap(basic_istream& __rhs) 728 { 729 __ios_type::swap(__rhs); 730 std::swap(_M_gcount, __rhs._M_gcount); 731 } 732 #endif 733 734 template<typename _ValueT> 735 __istream_type& 736 _M_extract(_ValueT& __v); 737 }; 738 739 /// Explicit specialization declarations, defined in src/istream.cc. 740 template<> 741 basic_istream<char>& 742 basic_istream<char>:: 743 getline(char_type* __s, streamsize __n, char_type __delim); 744 745 template<> 746 basic_istream<char>& 747 basic_istream<char>:: 748 ignore(streamsize __n); 749 750 template<> 751 basic_istream<char>& 752 basic_istream<char>:: 753 ignore(streamsize __n, int_type __delim); 754 755 #ifdef _GLIBCXX_USE_WCHAR_T 756 template<> 757 basic_istream<wchar_t>& 758 basic_istream<wchar_t>:: 759 getline(char_type* __s, streamsize __n, char_type __delim); 760 761 template<> 762 basic_istream<wchar_t>& 763 basic_istream<wchar_t>:: 764 ignore(streamsize __n); 765 766 template<> 767 basic_istream<wchar_t>& 768 basic_istream<wchar_t>:: 769 ignore(streamsize __n, int_type __delim); 770 #endif 771 772 /** 773 * @brief Performs setup work for input streams. 774 * 775 * Objects of this class are created before all of the standard 776 * extractors are run. It is responsible for <em>exception-safe 777 * prefix and suffix operations,</em> although only prefix actions 778 * are currently required by the standard. 779 */ 780 template<typename _CharT, typename _Traits> 781 class basic_istream<_CharT, _Traits>::sentry 782 { 783 // Data Members. 784 bool _M_ok; 785 786 public: 787 /// Easy access to dependent types. 788 typedef _Traits traits_type; 789 typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 790 typedef basic_istream<_CharT, _Traits> __istream_type; 791 typedef typename __istream_type::__ctype_type __ctype_type; 792 typedef typename _Traits::int_type __int_type; 793 794 /** 795 * @brief The constructor performs all the work. 796 * @param __is The input stream to guard. 797 * @param __noskipws Whether to consume whitespace or not. 798 * 799 * If the stream state is good (@a __is.good() is true), then the 800 * following actions are performed, otherwise the sentry state 801 * is false (<em>not okay</em>) and failbit is set in the 802 * stream state. 803 * 804 * The sentry's preparatory actions are: 805 * 806 * -# if the stream is tied to an output stream, @c is.tie()->flush() 807 * is called to synchronize the output sequence 808 * -# if @a __noskipws is false, and @c ios_base::skipws is set in 809 * @c is.flags(), the sentry extracts and discards whitespace 810 * characters from the stream. The currently imbued locale is 811 * used to determine whether each character is whitespace. 812 * 813 * If the stream state is still good, then the sentry state becomes 814 * true (@a okay). 815 */ 816 explicit 817 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false); 818 819 /** 820 * @brief Quick status checking. 821 * @return The sentry state. 822 * 823 * For ease of use, sentries may be converted to booleans. The 824 * return value is that of the sentry state (true == okay). 825 */ 826 #if __cplusplus >= 201103L 827 explicit 828 #endif 829 operator bool() const 830 { return _M_ok; } 831 }; 832 833 ///@{ 834 /** 835 * @brief Character extractors 836 * @param __in An input stream. 837 * @param __c A character reference. 838 * @return in 839 * 840 * Behaves like one of the formatted arithmetic extractors described in 841 * std::basic_istream. After constructing a sentry object with good 842 * status, this function extracts a character (if one is available) and 843 * stores it in @a __c. Otherwise, sets failbit in the input stream. 844 */ 845 template<typename _CharT, typename _Traits> 846 basic_istream<_CharT, _Traits>& 847 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c); 848 849 template<class _Traits> 850 inline basic_istream<char, _Traits>& 851 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c) 852 { return (__in >> reinterpret_cast<char&>(__c)); } 853 854 template<class _Traits> 855 inline basic_istream<char, _Traits>& 856 operator>>(basic_istream<char, _Traits>& __in, signed char& __c) 857 { return (__in >> reinterpret_cast<char&>(__c)); } 858 ///@} 859 860 861 template<typename _CharT, typename _Traits> 862 void 863 __istream_extract(basic_istream<_CharT, _Traits>&, _CharT*, streamsize); 864 865 void __istream_extract(istream&, char*, streamsize); 866 867 ///@{ 868 /** 869 * @brief Character string extractors 870 * @param __in An input stream. 871 * @param __s A character array (or a pointer to an array before C++20). 872 * @return __in 873 * 874 * Behaves like one of the formatted arithmetic extractors described in 875 * `std::basic_istream`. After constructing a sentry object with good 876 * status, this function extracts up to `n` characters and stores them 877 * into the array `__s`. `n` is defined as: 878 * 879 * - if `width()` is greater than zero, `n` is `min(width(), n)` 880 * - otherwise `n` is the number of elements of the array 881 * - (before C++20 the pointer is assumed to point to an array of 882 * the largest possible size for an array of `char_type`). 883 * 884 * Characters are extracted and stored until one of the following happens: 885 * - `n - 1` characters are stored 886 * - EOF is reached 887 * - the next character is whitespace according to the current locale 888 * 889 * `width(0)` is then called for the input stream. 890 * 891 * If no characters are extracted, sets failbit. 892 */ 893 894 #if __cplusplus <= 201703L 895 template<typename _CharT, typename _Traits> 896 __attribute__((__nonnull__(2), __access__(__write_only__, 2))) 897 inline basic_istream<_CharT, _Traits>& 898 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s) 899 { 900 #ifdef __OPTIMIZE__ 901 // Function inlining might make the buffer size known, allowing us to 902 // prevent overflow. 903 size_t __n = __builtin_object_size(__s, 0); 904 if (__n < sizeof(_CharT)) 905 { 906 // There is not even space for the required null terminator. 907 __glibcxx_assert(__n >= sizeof(_CharT)); 908 // No point calling __istream_extract, but still need to reset width. 909 __in.width(0); 910 __in.setstate(ios_base::failbit); 911 } 912 else if (__n != (size_t)-1) 913 { 914 __n /= sizeof(_CharT); 915 streamsize __w = __in.width(); 916 std::__istream_extract(__in, __s, __n); 917 if (__in.good() && (__w <= 0 || __n < (size_t)__w)) 918 { 919 // Stopped extracting early to avoid overflowing the buffer, 920 // but might have stopped anyway (and set eofbit) if at EOF. 921 const typename _Traits::int_type __c = __in.rdbuf()->sgetc(); 922 const bool __eof = _Traits::eq_int_type(__c, _Traits::eof()); 923 if (__builtin_expect(__eof, true)) // Assume EOF, not overflow. 924 __in.setstate(ios_base::eofbit); 925 } 926 } 927 else 928 #endif // __OPTIMIZE 929 { 930 // Buffer size is unknown, have to assume it's huge. 931 streamsize __n = __gnu_cxx::__numeric_traits<streamsize>::__max; 932 __n /= sizeof(_CharT); 933 std::__istream_extract(__in, __s, __n); 934 } 935 return __in; 936 } 937 938 template<class _Traits> 939 __attribute__((__nonnull__(2), __access__(__write_only__, 2))) 940 inline basic_istream<char, _Traits>& 941 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s) 942 { return __in >> reinterpret_cast<char*>(__s); } 943 944 template<class _Traits> 945 __attribute__((__nonnull__(2), __access__(__write_only__, 2))) 946 inline basic_istream<char, _Traits>& 947 operator>>(basic_istream<char, _Traits>& __in, signed char* __s) 948 { return __in >> reinterpret_cast<char*>(__s); } 949 #else 950 // _GLIBCXX_RESOLVE_LIB_DEFECTS 951 // 2499. operator>>(istream&, char*) makes it hard to avoid buffer overflows 952 template<typename _CharT, typename _Traits, size_t _Num> 953 inline basic_istream<_CharT, _Traits>& 954 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT (&__s)[_Num]) 955 { 956 static_assert(_Num <= __gnu_cxx::__numeric_traits<streamsize>::__max); 957 std::__istream_extract(__in, __s, _Num); 958 return __in; 959 } 960 961 template<class _Traits, size_t _Num> 962 inline basic_istream<char, _Traits>& 963 operator>>(basic_istream<char, _Traits>& __in, unsigned char (&__s)[_Num]) 964 { return __in >> reinterpret_cast<char(&)[_Num]>(__s); } 965 966 template<class _Traits, size_t _Num> 967 inline basic_istream<char, _Traits>& 968 operator>>(basic_istream<char, _Traits>& __in, signed char (&__s)[_Num]) 969 { return __in >> reinterpret_cast<char(&)[_Num]>(__s); } 970 #endif 971 ///@} 972 973 /** 974 * @brief Template class basic_iostream 975 * @ingroup io 976 * 977 * @tparam _CharT Type of character stream. 978 * @tparam _Traits Traits for character type, defaults to 979 * char_traits<_CharT>. 980 * 981 * This class multiply inherits from the input and output stream classes 982 * simply to provide a single interface. 983 */ 984 template<typename _CharT, typename _Traits> 985 class basic_iostream 986 : public basic_istream<_CharT, _Traits>, 987 public basic_ostream<_CharT, _Traits> 988 { 989 public: 990 // _GLIBCXX_RESOLVE_LIB_DEFECTS 991 // 271. basic_iostream missing typedefs 992 // Types (inherited): 993 typedef _CharT char_type; 994 typedef typename _Traits::int_type int_type; 995 typedef typename _Traits::pos_type pos_type; 996 typedef typename _Traits::off_type off_type; 997 typedef _Traits traits_type; 998 999 // Non-standard Types: 1000 typedef basic_istream<_CharT, _Traits> __istream_type; 1001 typedef basic_ostream<_CharT, _Traits> __ostream_type; 1002 1003 /** 1004 * @brief Constructor does nothing. 1005 * 1006 * Both of the parent classes are initialized with the same 1007 * streambuf pointer passed to this constructor. 1008 */ 1009 explicit 1010 basic_iostream(basic_streambuf<_CharT, _Traits>* __sb) 1011 : __istream_type(__sb), __ostream_type(__sb) { } 1012 1013 /** 1014 * @brief Destructor does nothing. 1015 */ 1016 virtual 1017 ~basic_iostream() { } 1018 1019 protected: 1020 basic_iostream() 1021 : __istream_type(), __ostream_type() { } 1022 1023 #if __cplusplus >= 201103L 1024 basic_iostream(const basic_iostream&) = delete; 1025 1026 basic_iostream(basic_iostream&& __rhs) 1027 : __istream_type(std::move(__rhs)), __ostream_type(*this) 1028 { } 1029 1030 // 27.7.3.3 Assign/swap 1031 1032 basic_iostream& operator=(const basic_iostream&) = delete; 1033 1034 basic_iostream& 1035 operator=(basic_iostream&& __rhs) 1036 { 1037 swap(__rhs); 1038 return *this; 1039 } 1040 1041 void 1042 swap(basic_iostream& __rhs) 1043 { __istream_type::swap(__rhs); } 1044 #endif 1045 }; 1046 1047 /** 1048 * @brief Quick and easy way to eat whitespace 1049 * 1050 * This manipulator extracts whitespace characters, stopping when the 1051 * next character is non-whitespace, or when the input sequence is empty. 1052 * If the sequence is empty, @c eofbit is set in the stream, but not 1053 * @c failbit. 1054 * 1055 * The current locale is used to distinguish whitespace characters. 1056 * 1057 * Example: 1058 * @code 1059 * MyClass mc; 1060 * 1061 * std::cin >> std::ws >> mc; 1062 * @endcode 1063 * will skip leading whitespace before calling operator>> on cin and your 1064 * object. Note that the same effect can be achieved by creating a 1065 * std::basic_istream::sentry inside your definition of operator>>. 1066 */ 1067 template<typename _CharT, typename _Traits> 1068 basic_istream<_CharT, _Traits>& 1069 ws(basic_istream<_CharT, _Traits>& __is); 1070 1071 #if __cplusplus >= 201103L 1072 // C++11 27.7.2.6 Rvalue stream extraction [istream.rvalue] 1073 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1074 // 2328. Rvalue stream extraction should use perfect forwarding 1075 // 1203. More useful rvalue stream insertion 1076 1077 #if __cpp_concepts >= 201907L && __glibcxx_type_trait_variable_templates 1078 template<typename _Is, typename _Tp> 1079 requires __derived_from_ios_base<_Is> 1080 && requires (_Is& __is, _Tp&& __t) { __is >> std::forward<_Tp>(__t); } 1081 using __rvalue_stream_extraction_t = _Is&&; 1082 #else 1083 template<typename _Is, typename _Tp, 1084 typename = _Require_derived_from_ios_base<_Is>, 1085 typename = decltype(std::declval<_Is&>() >> std::declval<_Tp>())> 1086 using __rvalue_stream_extraction_t = _Is&&; 1087 #endif 1088 1089 /** 1090 * @brief Generic extractor for rvalue stream 1091 * @param __is An input stream. 1092 * @param __x A reference to the extraction target. 1093 * @return __is 1094 * 1095 * This is just a forwarding function to allow extraction from 1096 * rvalue streams since they won't bind to the extractor functions 1097 * that take an lvalue reference. 1098 */ 1099 template<typename _Istream, typename _Tp> 1100 inline __rvalue_stream_extraction_t<_Istream, _Tp> 1101 operator>>(_Istream&& __is, _Tp&& __x) 1102 { 1103 __is >> std::forward<_Tp>(__x); 1104 return std::move(__is); 1105 } 1106 #endif // C++11 1107 1108 _GLIBCXX_END_NAMESPACE_VERSION 1109 } // namespace 1110 1111 #include <bits/istream.tcc> 1112 1113 #endif /* _GLIBCXX_ISTREAM */