libstdc++: istream Source File

libstdc++

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