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 __istream_type&
560 ignore(streamsize __n, char __delim) requires same_as<_CharT, char>
561 { return ignore(__n, traits_type::to_int_type(__delim)); }
562#endif
563
564 /**
565 * @brief Looking ahead in the stream
566 * @return The next character, or eof().
567 *
568 * If, after constructing the sentry object, @c good() is false,
569 * returns @c traits::eof(). Otherwise reads but does not extract
570 * the next input character.
571 */
574
575 /**
576 * @brief Extraction without delimiters.
577 * @param __s A character array.
578 * @param __n Maximum number of characters to store.
579 * @return *this
580 *
581 * If the stream state is @c good(), extracts characters and stores
582 * them into @a __s until one of the following happens:
583 * - @a __n characters are stored
584 * - the input sequence reaches end-of-file, in which case the error
585 * state is set to @c failbit|eofbit.
586 *
587 * @note This function is not overloaded on signed char and
588 * unsigned char.
589 */
590 __istream_type&
591 read(char_type* __s, streamsize __n);
592
593 /**
594 * @brief Extraction until the buffer is exhausted, but no more.
595 * @param __s A character array.
596 * @param __n Maximum number of characters to store.
597 * @return The number of characters extracted.
598 *
599 * Extracts characters and stores them into @a __s depending on the
600 * number of characters remaining in the streambuf's buffer,
601 * @c rdbuf()->in_avail(), called @c A here:
602 * - if @c A @c == @c -1, sets eofbit and extracts no characters
603 * - if @c A @c == @c 0, extracts no characters
604 * - if @c A @c > @c 0, extracts @c min(A,n)
605 *
606 * The goal is to empty the current buffer, and to not request any
607 * more from the external input sequence controlled by the streambuf.
608 */
610 readsome(char_type* __s, streamsize __n);
611
612 /**
613 * @brief Unextracting a single character.
614 * @param __c The character to push back into the input stream.
615 * @return *this
616 *
617 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
618 *
619 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
620 * the error state.
621 *
622 * @note This function first clears eofbit. Since no characters
623 * are extracted, the next call to @c gcount() will return 0,
624 * as required by DR 60.
625 */
626 __istream_type&
627 putback(char_type __c);
628
629 /**
630 * @brief Unextracting the previous character.
631 * @return *this
632 *
633 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
634 *
635 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
636 * the error state.
637 *
638 * @note This function first clears eofbit. Since no characters
639 * are extracted, the next call to @c gcount() will return 0,
640 * as required by DR 60.
641 */
642 __istream_type&
644
645 /**
646 * @brief Synchronizing the stream buffer.
647 * @return 0 on success, -1 on failure
648 *
649 * If @c rdbuf() is a null pointer, returns -1.
650 *
651 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
652 * sets badbit and returns -1.
653 *
654 * Otherwise, returns 0.
655 *
656 * @note This function does not count the number of characters
657 * extracted, if any, and therefore does not affect the next
658 * call to @c gcount().
659 */
660 int
662
663 /**
664 * @brief Getting the current read position.
665 * @return A file position object.
666 *
667 * If @c fail() is not false, returns @c pos_type(-1) to indicate
668 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
669 *
670 * @note This function does not count the number of characters
671 * extracted, if any, and therefore does not affect the next
672 * call to @c gcount(). At variance with putback, unget and
673 * seekg, eofbit is not cleared first.
674 */
675 pos_type
677
678 /**
679 * @brief Changing the current read position.
680 * @param __pos A file position object.
681 * @return *this
682 *
683 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(__pos). If
684 * that function fails, sets failbit.
685 *
686 * @note This function first clears eofbit. It does not count the
687 * number of characters extracted, if any, and therefore does
688 * not affect the next call to @c gcount().
689 */
690 __istream_type&
691 seekg(pos_type);
692
693 /**
694 * @brief Changing the current read position.
695 * @param __off A file offset object.
696 * @param __dir The direction in which to seek.
697 * @return *this
698 *
699 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(__off,__dir).
700 * If that function fails, sets failbit.
701 *
702 * @note This function first clears eofbit. It does not count the
703 * number of characters extracted, if any, and therefore does
704 * not affect the next call to @c gcount().
705 */
706 __istream_type&
708 ///@}
709
710 protected:
713 { this->init(0); }
714
715#if __cplusplus >= 201103L
716 basic_istream(const basic_istream&) = delete;
717
719 : __ios_type(), _M_gcount(__rhs._M_gcount)
720 {
721 __ios_type::move(__rhs);
722 __rhs._M_gcount = 0;
723 }
724
725 // 27.7.3.3 Assign/swap
726
727 basic_istream& operator=(const basic_istream&) = delete;
728
730 operator=(basic_istream&& __rhs)
731 {
732 swap(__rhs);
733 return *this;
734 }
735
736 void
737 swap(basic_istream& __rhs)
738 {
739 __ios_type::swap(__rhs);
740 std::swap(_M_gcount, __rhs._M_gcount);
741 }
742#endif
743
744 template<typename _ValueT>
745 __istream_type&
746 _M_extract(_ValueT& __v);
747 };
748
749 /// Explicit specialization declarations, defined in src/istream.cc.
750 template<>
753 getline(char_type* __s, streamsize __n, char_type __delim);
754
755 template<>
759
760 template<>
763 ignore(streamsize __n, int_type __delim);
764
765#ifdef _GLIBCXX_USE_WCHAR_T
766 template<>
769 getline(char_type* __s, streamsize __n, char_type __delim);
770
771 template<>
775
776 template<>
779 ignore(streamsize __n, int_type __delim);
780#endif
781
782 /**
783 * @brief Performs setup work for input streams.
784 *
785 * Objects of this class are created before all of the standard
786 * extractors are run. It is responsible for <em>exception-safe
787 * prefix and suffix operations,</em> although only prefix actions
788 * are currently required by the standard.
789 */
790 template<typename _CharT, typename _Traits>
791 class basic_istream<_CharT, _Traits>::sentry
792 {
793 // Data Members.
794 bool _M_ok;
795
796 public:
797 /// Easy access to dependent types.
798 typedef _Traits traits_type;
799 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
800 typedef basic_istream<_CharT, _Traits> __istream_type;
801 typedef typename __istream_type::__ctype_type __ctype_type;
802 typedef typename _Traits::int_type __int_type;
803
804 /**
805 * @brief The constructor performs all the work.
806 * @param __is The input stream to guard.
807 * @param __noskipws Whether to consume whitespace or not.
808 *
809 * If the stream state is good (@a __is.good() is true), then the
810 * following actions are performed, otherwise the sentry state
811 * is false (<em>not okay</em>) and failbit is set in the
812 * stream state.
813 *
814 * The sentry's preparatory actions are:
815 *
816 * -# if the stream is tied to an output stream, @c is.tie()->flush()
817 * is called to synchronize the output sequence
818 * -# if @a __noskipws is false, and @c ios_base::skipws is set in
819 * @c is.flags(), the sentry extracts and discards whitespace
820 * characters from the stream. The currently imbued locale is
821 * used to determine whether each character is whitespace.
822 *
823 * If the stream state is still good, then the sentry state becomes
824 * true (@a okay).
825 */
826 explicit
827 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
828
829 /**
830 * @brief Quick status checking.
831 * @return The sentry state.
832 *
833 * For ease of use, sentries may be converted to booleans. The
834 * return value is that of the sentry state (true == okay).
835 */
836#if __cplusplus >= 201103L
837 explicit
838#endif
839 operator bool() const
840 { return _M_ok; }
841 };
842
843 ///@{
844 /**
845 * @brief Character extractors
846 * @param __in An input stream.
847 * @param __c A character reference.
848 * @return in
849 *
850 * Behaves like one of the formatted arithmetic extractors described in
851 * std::basic_istream. After constructing a sentry object with good
852 * status, this function extracts a character (if one is available) and
853 * stores it in @a __c. Otherwise, sets failbit in the input stream.
854 */
855 template<typename _CharT, typename _Traits>
857 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
858
859 template<class _Traits>
861 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
862 { return (__in >> reinterpret_cast<char&>(__c)); }
863
864 template<class _Traits>
865 inline basic_istream<char, _Traits>&
867 { return (__in >> reinterpret_cast<char&>(__c)); }
868 ///@}
869
870
871 template<typename _CharT, typename _Traits>
872 void
873 __istream_extract(basic_istream<_CharT, _Traits>&, _CharT*, streamsize);
874
875 void __istream_extract(istream&, char*, streamsize);
876
877 ///@{
878 /**
879 * @brief Character string extractors
880 * @param __in An input stream.
881 * @param __s A character array (or a pointer to an array before C++20).
882 * @return __in
883 *
884 * Behaves like one of the formatted arithmetic extractors described in
885 * `std::basic_istream`. After constructing a sentry object with good
886 * status, this function extracts up to `n` characters and stores them
887 * into the array `__s`. `n` is defined as:
888 *
889 * - if `width()` is greater than zero, `n` is `min(width(), n)`
890 * - otherwise `n` is the number of elements of the array
891 * - (before C++20 the pointer is assumed to point to an array of
892 * the largest possible size for an array of `char_type`).
893 *
894 * Characters are extracted and stored until one of the following happens:
895 * - `n - 1` characters are stored
896 * - EOF is reached
897 * - the next character is whitespace according to the current locale
898 *
899 * `width(0)` is then called for the input stream.
900 *
901 * If no characters are extracted, sets failbit.
902 */
903
904#if __cplusplus <= 201703L
905 template<typename _CharT, typename _Traits>
906 __attribute__((__nonnull__(2), __access__(__write_only__, 2)))
907 inline basic_istream<_CharT, _Traits>&
908 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
909 {
910#ifdef __OPTIMIZE__
911 // Function inlining might make the buffer size known, allowing us to
912 // prevent overflow.
913 size_t __n = __builtin_object_size(__s, 0);
914 if (__n < sizeof(_CharT))
915 {
916 // There is not even space for the required null terminator.
917 __glibcxx_assert(__n >= sizeof(_CharT));
918 // No point calling __istream_extract, but still need to reset width.
919 __in.width(0);
920 __in.setstate(ios_base::failbit);
921 }
922 else if (__n != (size_t)-1)
923 {
924 __n /= sizeof(_CharT);
925 streamsize __w = __in.width();
926 std::__istream_extract(__in, __s, __n);
927 if (__in.good() && (__w <= 0 || __n < (size_t)__w))
928 {
929 // Stopped extracting early to avoid overflowing the buffer,
930 // but might have stopped anyway (and set eofbit) if at EOF.
931 const typename _Traits::int_type __c = __in.rdbuf()->sgetc();
932 const bool __eof = _Traits::eq_int_type(__c, _Traits::eof());
933 if (__builtin_expect(__eof, true)) // Assume EOF, not overflow.
934 __in.setstate(ios_base::eofbit);
935 }
936 }
937 else
938#endif // __OPTIMIZE
939 {
940 // Buffer size is unknown, have to assume it's huge.
941 streamsize __n = __gnu_cxx::__numeric_traits<streamsize>::__max;
942 __n /= sizeof(_CharT);
943 std::__istream_extract(__in, __s, __n);
944 }
945 return __in;
946 }
947
948 template<class _Traits>
949 __attribute__((__nonnull__(2), __access__(__write_only__, 2)))
950 inline basic_istream<char, _Traits>&
951 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
952 { return __in >> reinterpret_cast<char*>(__s); }
953
954 template<class _Traits>
955 __attribute__((__nonnull__(2), __access__(__write_only__, 2)))
956 inline basic_istream<char, _Traits>&
957 operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
958 { return __in >> reinterpret_cast<char*>(__s); }
959#else
960 // _GLIBCXX_RESOLVE_LIB_DEFECTS
961 // 2499. operator>>(istream&, char*) makes it hard to avoid buffer overflows
962 template<typename _CharT, typename _Traits, size_t _Num>
964 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT (&__s)[_Num])
965 {
966 static_assert(_Num <= __gnu_cxx::__numeric_traits<streamsize>::__max);
967 std::__istream_extract(__in, __s, _Num);
968 return __in;
969 }
970
971 template<class _Traits, size_t _Num>
972 inline basic_istream<char, _Traits>&
973 operator>>(basic_istream<char, _Traits>& __in, unsigned char (&__s)[_Num])
974 { return __in >> reinterpret_cast<char(&)[_Num]>(__s); }
975
976 template<class _Traits, size_t _Num>
977 inline basic_istream<char, _Traits>&
978 operator>>(basic_istream<char, _Traits>& __in, signed char (&__s)[_Num])
979 { return __in >> reinterpret_cast<char(&)[_Num]>(__s); }
980#endif
981 ///@}
982
983 /**
984 * @brief Template class basic_iostream
985 * @ingroup io
986 *
987 * @tparam _CharT Type of character stream.
988 * @tparam _Traits Traits for character type, defaults to
989 * char_traits<_CharT>.
990 *
991 * This class multiply inherits from the input and output stream classes
992 * simply to provide a single interface.
993 */
994 template<typename _CharT, typename _Traits>
996 : public basic_istream<_CharT, _Traits>,
997 public basic_ostream<_CharT, _Traits>
998 {
999 public:
1000 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1001 // 271. basic_iostream missing typedefs
1002 // Types (inherited):
1003 typedef _CharT char_type;
1004 typedef typename _Traits::int_type int_type;
1005 typedef typename _Traits::pos_type pos_type;
1006 typedef typename _Traits::off_type off_type;
1007 typedef _Traits traits_type;
1008
1009 // Non-standard Types:
1010 typedef basic_istream<_CharT, _Traits> __istream_type;
1011 typedef basic_ostream<_CharT, _Traits> __ostream_type;
1012
1013 /**
1014 * @brief Constructor does nothing.
1015 *
1016 * Both of the parent classes are initialized with the same
1017 * streambuf pointer passed to this constructor.
1018 */
1019 explicit
1021 : __istream_type(__sb), __ostream_type(__sb) { }
1022
1023 /**
1024 * @brief Destructor does nothing.
1025 */
1026 virtual
1028
1029 protected:
1031 : __istream_type(), __ostream_type() { }
1032
1033#if __cplusplus >= 201103L
1034 basic_iostream(const basic_iostream&) = delete;
1035
1037 : __istream_type(std::move(__rhs)), __ostream_type(*this)
1038 { }
1039
1040 // 27.7.3.3 Assign/swap
1041
1042 basic_iostream& operator=(const basic_iostream&) = delete;
1043
1045 operator=(basic_iostream&& __rhs)
1046 {
1047 swap(__rhs);
1048 return *this;
1049 }
1050
1051 void
1052 swap(basic_iostream& __rhs)
1053 { __istream_type::swap(__rhs); }
1054#endif
1055 };
1056
1057 /**
1058 * @brief Quick and easy way to eat whitespace
1059 *
1060 * This manipulator extracts whitespace characters, stopping when the
1061 * next character is non-whitespace, or when the input sequence is empty.
1062 * If the sequence is empty, @c eofbit is set in the stream, but not
1063 * @c failbit.
1064 *
1065 * The current locale is used to distinguish whitespace characters.
1066 *
1067 * Example:
1068 * @code
1069 * MyClass mc;
1070 *
1071 * std::cin >> std::ws >> mc;
1072 * @endcode
1073 * will skip leading whitespace before calling operator>> on cin and your
1074 * object. Note that the same effect can be achieved by creating a
1075 * std::basic_istream::sentry inside your definition of operator>>.
1076 */
1077 template<typename _CharT, typename _Traits>
1080
1081#if __cplusplus >= 201103L
1082 // C++11 27.7.2.6 Rvalue stream extraction [istream.rvalue]
1083 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1084 // 2328. Rvalue stream extraction should use perfect forwarding
1085 // 1203. More useful rvalue stream insertion
1086
1087#if __cpp_concepts >= 201907L && __glibcxx_type_trait_variable_templates
1088 template<typename _Is, typename _Tp>
1089 requires __derived_from_ios_base<_Is>
1090 && requires (_Is& __is, _Tp&& __t) { __is >> std::forward<_Tp>(__t); }
1091 using __rvalue_stream_extraction_t = _Is&&;
1092#else
1093 template<typename _Is, typename _Tp,
1094 typename = _Require_derived_from_ios_base<_Is>,
1095 typename = decltype(std::declval<_Is&>() >> std::declval<_Tp>())>
1096 using __rvalue_stream_extraction_t = _Is&&;
1097#endif
1098
1099 /**
1100 * @brief Generic extractor for rvalue stream
1101 * @param __is An input stream.
1102 * @param __x A reference to the extraction target.
1103 * @return __is
1104 *
1105 * This is just a forwarding function to allow extraction from
1106 * rvalue streams since they won't bind to the extractor functions
1107 * that take an lvalue reference.
1108 */
1109 template<typename _Istream, typename _Tp>
1110 inline __rvalue_stream_extraction_t<_Istream, _Tp>
1111 operator>>(_Istream&& __is, _Tp&& __x)
1112 {
1113 __is >> std::forward<_Tp>(__x);
1114 return std::move(__is);
1115 }
1116#endif // C++11
1117
1118_GLIBCXX_END_NAMESPACE_VERSION
1119} // namespace
1120
1121#include <bits/istream.tcc>
1122
1123#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:1020
virtual ~basic_iostream()
Destructor does nothing.
Definition istream:1027
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:798
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.