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