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