libstdc++: sstream Source File

libstdc++

sstream
Go to the documentation of this file.
1// String based 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/** @file include/sstream
26 * This is a Standard C++ Library header.
27 */
28
29//
30// ISO C++ 14882: 27.7 String-based streams
31//
32
33#ifndef _GLIBCXX_SSTREAM
34#define _GLIBCXX_SSTREAM 1
35
36#ifdef _GLIBCXX_SYSHDR
37#pragma GCC system_header
38#endif
39
40#include <bits/requires_hosted.h> // iostream
41
42#include <istream>
43#include <ostream>
44
45#include <bits/iosfwd_string.h>
46#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
47
48#define __glibcxx_want_sstream_from_string_view
49#include <bits/version.h>
50
51#ifdef __cpp_lib_sstream_from_string_view
52# include <string_view>
53#endif
54
55#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
56# define _GLIBCXX_LVAL_REF_QUAL &
57# define _GLIBCXX_SSTREAM_ALWAYS_INLINE
58#else
59# define _GLIBCXX_LVAL_REF_QUAL
60// For symbols that are not exported from libstdc++.so for the COW string ABI.
61# define _GLIBCXX_SSTREAM_ALWAYS_INLINE [[__gnu__::__always_inline__]]
62#endif
63
64namespace std _GLIBCXX_VISIBILITY(default)
65{
66_GLIBCXX_BEGIN_NAMESPACE_VERSION
67_GLIBCXX_BEGIN_NAMESPACE_CXX11
68
69 // [27.7.1] template class basic_stringbuf
70 /**
71 * @brief The actual work of input and output (for std::string).
72 * @ingroup io
73 *
74 * @tparam _CharT Type of character stream.
75 * @tparam _Traits Traits for character type, defaults to
76 * char_traits<_CharT>.
77 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
78 *
79 * This class associates either or both of its input and output sequences
80 * with a sequence of characters, which can be initialized from, or made
81 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
82 *
83 * For this class, open modes (of type @c ios_base::openmode) have
84 * @c in set if the input sequence can be read, and @c out set if the
85 * output sequence can be written.
86 */
87 template<typename _CharT, typename _Traits, typename _Alloc>
88 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
89 {
90 struct __xfer_bufptrs;
91
92#if __cplusplus >= 201103L
93 using allocator_traits = std::allocator_traits<_Alloc>;
94 using _Noexcept_swap
95 = __or_<typename allocator_traits::propagate_on_container_swap,
96 typename allocator_traits::is_always_equal>;
97#endif
98
99 public:
100 // Types:
101 typedef _CharT char_type;
102 typedef _Traits traits_type;
103 // _GLIBCXX_RESOLVE_LIB_DEFECTS
104 // 251. basic_stringbuf missing allocator_type
105 typedef _Alloc allocator_type;
106 typedef typename traits_type::int_type int_type;
107 typedef typename traits_type::pos_type pos_type;
108 typedef typename traits_type::off_type off_type;
109
110 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
111 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
112 typedef typename __string_type::size_type __size_type;
113
114 protected:
115 /// Place to stash in || out || in | out settings for current stringbuf.
116 ios_base::openmode _M_mode;
117
118 // Data Members:
119 __string_type _M_string;
120
121 public:
122 // Constructors:
123
124 /**
125 * @brief Starts with an empty string buffer.
126 *
127 * The default constructor initializes the parent class using its
128 * own default ctor.
129 */
130 basic_stringbuf()
131 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
132 { }
133
134 /**
135 * @brief Starts with an empty string buffer.
136 * @param __mode Whether the buffer can read, or write, or both.
137 *
138 * The default constructor initializes the parent class using its
139 * own default ctor.
140 */
141 explicit
142 basic_stringbuf(ios_base::openmode __mode)
143 : __streambuf_type(), _M_mode(__mode), _M_string()
144 { }
145
146 /**
147 * @brief Starts with an existing string buffer.
148 * @param __str A string to copy as a starting buffer.
149 * @param __mode Whether the buffer can read, or write, or both.
150 *
151 * This constructor initializes the parent class using its
152 * own default ctor.
153 */
154 explicit
155 basic_stringbuf(const __string_type& __str,
156 ios_base::openmode __mode = ios_base::in | ios_base::out)
157 : __streambuf_type(), _M_mode(),
158 _M_string(__str.data(), __str.size(), __str.get_allocator())
159 { _M_stringbuf_init(__mode); }
160
161#if __cplusplus >= 201103L
162 basic_stringbuf(const basic_stringbuf&) = delete;
163
164 basic_stringbuf(basic_stringbuf&& __rhs)
165 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
166 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
167
168#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
169 // P0408 Efficient access to basic_stringbuf buffer
170 explicit
171 basic_stringbuf(const allocator_type& __a)
172 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
173 { }
174
175 basic_stringbuf(ios_base::openmode __mode,
176 const allocator_type& __a)
177 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
178 { }
179
180 explicit
181 basic_stringbuf(__string_type&& __s,
182 ios_base::openmode __mode = ios_base::in
183 | ios_base::out)
184 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
185 { _M_stringbuf_init(__mode); }
186
187 template<typename _SAlloc>
188 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
189 const allocator_type& __a)
190 : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a)
191 { }
192
193 template<typename _SAlloc>
194 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
195 ios_base::openmode __mode,
196 const allocator_type& __a)
197 : __streambuf_type(), _M_mode(__mode),
198 _M_string(__s.data(), __s.size(), __a)
199 { _M_stringbuf_init(__mode); }
200
201 template<typename _SAlloc>
202 explicit
203 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
204 ios_base::openmode __mode = ios_base::in
205 | ios_base::out)
206 : basic_stringbuf(__s, __mode, allocator_type{})
207 { }
208#endif
209
210#ifdef __cpp_lib_sstream_from_string_view
211 template<typename _Tp>
212 explicit
213 basic_stringbuf(const _Tp& __t,
214 ios_base::openmode __mode = ios_base::in | ios_base::out)
215 requires (is_convertible_v<const _Tp&,
216 basic_string_view<_CharT, _Traits>>)
217 : basic_stringbuf(__t, __mode, allocator_type{})
218 { }
219
220 template<typename _Tp>
221 basic_stringbuf(const _Tp& __t, const allocator_type& __a)
222 requires (is_convertible_v<const _Tp&,
223 basic_string_view<_CharT, _Traits>>)
224 : basic_stringbuf(__t, ios_base::in | ios_base::out, __a)
225 { }
226
227 template<typename _Tp>
228 basic_stringbuf(const _Tp& __t, ios_base::openmode __mode,
229 const allocator_type& __a)
230 requires (is_convertible_v<const _Tp&,
231 basic_string_view<_CharT, _Traits>>)
232 : _M_string(__t, __a)
233 { _M_stringbuf_init(__mode); }
234#endif // C++26
235
236#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
237 // P0408 Efficient access to basic_stringbuf buffer
238 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
239 : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this))
240 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
241
242 allocator_type get_allocator() const noexcept
243 { return _M_string.get_allocator(); }
244#endif // C++20
245
246 // 27.8.2.2 Assign and swap:
247
248 basic_stringbuf&
249 operator=(const basic_stringbuf&) = delete;
250
251 basic_stringbuf&
252 operator=(basic_stringbuf&& __rhs)
253 {
254 __xfer_bufptrs __st{__rhs, this};
255 const __streambuf_type& __base = __rhs;
256 __streambuf_type::operator=(__base);
257 this->pubimbue(__rhs.getloc());
258 _M_mode = __rhs._M_mode;
259 _M_string = std::move(__rhs._M_string);
260 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
261 return *this;
262 }
263
264 void
265 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
266 {
267 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
268 __xfer_bufptrs __r_st{__rhs, this};
269 __streambuf_type& __base = __rhs;
270 __streambuf_type::swap(__base);
271 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
272 std::swap(_M_mode, __rhs._M_mode);
273 std::swap(_M_string, __rhs._M_string); // XXX not exception safe
274 }
275#endif // C++11
276
277 // Getters and setters:
278
279 /**
280 * @brief Copying out the string buffer.
281 * @return A copy of one of the underlying sequences.
282 *
283 * <em>If the buffer is only created in input mode, the underlying
284 * character sequence is equal to the input sequence; otherwise, it
285 * is equal to the output sequence.</em> [27.7.1.2]/1
286 */
287 _GLIBCXX_NODISCARD
288 __string_type
289 str() const _GLIBCXX_LVAL_REF_QUAL
290 {
291 __string_type __ret(_M_string.get_allocator());
292 if (char_type* __hi = _M_high_mark())
293 __ret.assign(this->pbase(), __hi);
294 else
295 __ret = _M_string;
296 return __ret;
297 }
298
299#if __cplusplus > 201703L
300#if _GLIBCXX_USE_CXX11_ABI
301#if __cpp_concepts
302 // P0407 Allocator-aware basic_streambuf
303 template<__allocator_like _SAlloc>
304 _GLIBCXX_NODISCARD
305 basic_string<_CharT, _Traits, _SAlloc>
306 str(const _SAlloc& __sa) const
307 {
308 auto __sv = view();
309 return { __sv.data(), __sv.size(), __sa };
310 }
311#endif
312
313 _GLIBCXX_NODISCARD
314 __string_type
315 str() &&
316 {
317 if (char_type* __hi = _M_high_mark())
318 {
319 if (_M_string.data() == this->pbase()) [[likely]]
320 // Set length to end of sequence and add null terminator.
321 _M_string._M_set_length(__hi - this->pbase());
322 else
323 _M_string.assign(this->pbase(), __hi);
324 }
325 auto __str = std::move(_M_string);
326 _M_string.clear();
327 _M_sync(_M_string.data(), 0, 0);
328 return __str;
329 }
330#endif // cxx11 ABI
331
332 _GLIBCXX_SSTREAM_ALWAYS_INLINE
333 basic_string_view<char_type, traits_type>
334 view() const noexcept
335 {
336 if (char_type* __hi = _M_high_mark())
337 return { this->pbase(), __hi };
338 else
339 return _M_string;
340 }
341#endif // C++20
342
343 /**
344 * @brief Setting a new buffer.
345 * @param __s The string to use as a new sequence.
346 *
347 * Deallocates any previous stored sequence, then copies @a s to
348 * use as a new one.
349 */
350 void
351 str(const __string_type& __s)
352 {
353 // Cannot use _M_string = __s, since v3 strings are COW
354 // (not always true now but assign() always works).
355 _M_string.assign(__s.data(), __s.size());
356 _M_stringbuf_init(_M_mode);
357 }
358
359#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
360#if __cpp_concepts
361 // P0407 Allocator-aware basic_streambuf
362 template<__allocator_like _SAlloc>
363 requires (!is_same_v<_SAlloc, _Alloc>)
364 void
365 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
366 {
367 _M_string.assign(__s.data(), __s.size());
368 _M_stringbuf_init(_M_mode);
369 }
370#endif
371
372 void
373 str(__string_type&& __s)
374 {
375 _M_string = std::move(__s);
376 _M_stringbuf_init(_M_mode);
377 }
378#endif
379
380#ifdef __cpp_lib_sstream_from_string_view
381 template <typename _Tp>
382 void
383 str(const _Tp& __t)
384 requires (is_convertible_v<const _Tp&,
385 basic_string_view<_CharT, _Traits>>)
386 {
387 basic_string_view<_CharT, _Traits> __sv{__t};
388 _M_string = __sv;
389 _M_stringbuf_init(_M_mode);
390 }
391#endif // C++26
392
393 protected:
394 // Common initialization code goes here.
395 void
396 _M_stringbuf_init(ios_base::openmode __mode)
397 {
398 _M_mode = __mode;
399 __size_type __len = 0;
400 if (_M_mode & (ios_base::ate | ios_base::app))
401 __len = _M_string.size();
402 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
403 }
404
405 virtual streamsize
406 showmanyc()
407 {
408 streamsize __ret = -1;
409 if (_M_mode & ios_base::in)
410 {
411 _M_update_egptr();
412 __ret = this->egptr() - this->gptr();
413 }
414 return __ret;
415 }
416
417 virtual int_type
418 underflow();
419
420 virtual int_type
421 pbackfail(int_type __c = traits_type::eof());
422
423 virtual int_type
424 overflow(int_type __c = traits_type::eof());
425
426 /**
427 * @brief Manipulates the buffer.
428 * @param __s Pointer to a buffer area.
429 * @param __n Size of @a __s.
430 * @return @c this
431 *
432 * If no buffer has already been created, and both @a __s and @a __n are
433 * non-zero, then @c __s is used as a buffer; see
434 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
435 * for more.
436 */
437 virtual __streambuf_type*
438 setbuf(char_type* __s, streamsize __n)
439 {
440 if (__s && __n >= 0)
441 {
442 // This is implementation-defined behavior, and assumes
443 // that an external char_type array of length __n exists
444 // and has been pre-allocated. If this is not the case,
445 // things will quickly blow up.
446
447 // Step 1: Destroy the current internal array.
448 _M_string.clear();
449
450 // Step 2: Use the external array.
451 _M_sync(__s, __n, 0);
452 }
453 return this;
454 }
455
456 virtual pos_type
457 seekoff(off_type __off, ios_base::seekdir __way,
458 ios_base::openmode __mode = ios_base::in | ios_base::out);
459
460 virtual pos_type
461 seekpos(pos_type __sp,
462 ios_base::openmode __mode = ios_base::in | ios_base::out);
463
464 // Internal function for correctly updating the internal buffer
465 // for a particular _M_string, due to initialization or re-sizing
466 // of an existing _M_string.
467 void
468 _M_sync(char_type* __base, __size_type __i, __size_type __o);
469
470 // Internal function for correctly updating egptr() to the actual
471 // string end.
472 void
473 _M_update_egptr()
474 {
475 if (char_type* __pptr = this->pptr())
476 {
477 char_type* __egptr = this->egptr();
478 if (!__egptr || __pptr > __egptr)
479 {
480 if (_M_mode & ios_base::in)
481 this->setg(this->eback(), this->gptr(), __pptr);
482 else
483 this->setg(__pptr, __pptr, __pptr);
484 }
485 }
486 }
487
488 // Works around the issue with pbump, part of the protected
489 // interface of basic_streambuf, taking just an int.
490 void
491 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
492
493 private:
494 // Return a pointer to the end of the underlying character sequence.
495 // This might not be the same character as _M_string.end() because
496 // basic_stringbuf::overflow might have written to unused capacity
497 // in _M_string without updating its length.
498 __attribute__((__always_inline__))
499 char_type*
500 _M_high_mark() const _GLIBCXX_NOEXCEPT
501 {
502 if (char_type* __pptr = this->pptr())
503 {
504 char_type* __egptr = this->egptr();
505 if (!__egptr || __pptr > __egptr)
506 return __pptr; // Underlying sequence is [pbase, pptr).
507 else
508 return __egptr; // Underlying sequence is [pbase, egptr).
509 }
510 return 0; // Underlying character sequence is just _M_string.
511 }
512
513#if __cplusplus >= 201103L
514#if _GLIBCXX_USE_CXX11_ABI
515 // This type captures the state of the gptr / pptr pointers as offsets
516 // so they can be restored in another object after moving the string.
517 struct __xfer_bufptrs
518 {
519 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
520 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
521 {
522 const _CharT* const __str = __from._M_string.data();
523 const _CharT* __end = nullptr;
524 if (__from.eback())
525 {
526 _M_goff[0] = __from.eback() - __str;
527 _M_goff[1] = __from.gptr() - __str;
528 _M_goff[2] = __from.egptr() - __str;
529 __end = __from.egptr();
530 }
531 if (__from.pbase())
532 {
533 _M_poff[0] = __from.pbase() - __str;
534 _M_poff[1] = __from.pptr() - __from.pbase();
535 _M_poff[2] = __from.epptr() - __str;
536 if (!__end || __from.pptr() > __end)
537 __end = __from.pptr();
538 }
539
540 // Set _M_string length to the greater of the get and put areas.
541 if (__end)
542 {
543 // The const_cast avoids changing this constructor's signature,
544 // because it is exported from the dynamic library.
545 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
546 __mut_from._M_string._M_length(__end - __str);
547 }
548 }
549
550 ~__xfer_bufptrs()
551 {
552 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
553 if (_M_goff[0] != -1)
554 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
555 if (_M_poff[0] != -1)
556 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
557 }
558
559 basic_stringbuf* _M_to;
560 off_type _M_goff[3];
561 off_type _M_poff[3];
562 };
563#else
564 // This type does nothing when using Copy-On-Write strings.
565 struct __xfer_bufptrs
566 {
567 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
568 };
569#endif
570
571 // The move constructor initializes an __xfer_bufptrs temporary then
572 // delegates to this constructor to performs moves during its lifetime.
573 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
574 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
575 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
576 { }
577
578#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
579 // P0408 Efficient access to basic_stringbuf buffer
580
581 // The move constructor initializes an __xfer_bufptrs temporary then
582 // delegates to this constructor to performs moves during its lifetime.
583 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
584 __xfer_bufptrs&&)
585 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
586 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
587 { }
588#endif
589#endif // C++11
590 };
591
592
593 // [27.7.2] Template class basic_istringstream
594 /**
595 * @brief Controlling input for std::string.
596 * @ingroup io
597 *
598 * @tparam _CharT Type of character stream.
599 * @tparam _Traits Traits for character type, defaults to
600 * char_traits<_CharT>.
601 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
602 *
603 * This class supports reading from objects of type std::basic_string,
604 * using the inherited functions from std::basic_istream. To control
605 * the associated sequence, an instance of std::basic_stringbuf is used,
606 * which this page refers to as @c sb.
607 */
608 template<typename _CharT, typename _Traits, typename _Alloc>
609 class basic_istringstream : public basic_istream<_CharT, _Traits>
610 {
611 public:
612 // Types:
613 typedef _CharT char_type;
614 typedef _Traits traits_type;
615 // _GLIBCXX_RESOLVE_LIB_DEFECTS
616 // 251. basic_stringbuf missing allocator_type
617 typedef _Alloc allocator_type;
618 typedef typename traits_type::int_type int_type;
619 typedef typename traits_type::pos_type pos_type;
620 typedef typename traits_type::off_type off_type;
621
622 // Non-standard types:
623 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
624 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
625 typedef basic_istream<char_type, traits_type> __istream_type;
626
627 private:
628 __stringbuf_type _M_stringbuf;
629
630 public:
631 // Constructors:
632
633 /**
634 * @brief Default constructor starts with an empty string buffer.
635 *
636 * Initializes @c sb using @c in, and passes @c &sb to the base
637 * class initializer. Does not allocate any buffer.
638 *
639 * That's a lie. We initialize the base class with NULL, because the
640 * string class does its own memory management.
641 */
642 basic_istringstream()
643 : __istream_type(), _M_stringbuf(ios_base::in)
644 { this->init(std::__addressof(_M_stringbuf)); }
645
646 /**
647 * @brief Starts with an empty string buffer.
648 * @param __mode Whether the buffer can read, or write, or both.
649 *
650 * @c ios_base::in is automatically included in @a __mode.
651 *
652 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
653 * class initializer. Does not allocate any buffer.
654 *
655 * That's a lie. We initialize the base class with NULL, because the
656 * string class does its own memory management.
657 */
658 explicit
659 basic_istringstream(ios_base::openmode __mode)
660 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
661 { this->init(std::__addressof(_M_stringbuf)); }
662
663 /**
664 * @brief Starts with an existing string buffer.
665 * @param __str A string to copy as a starting buffer.
666 * @param __mode Whether the buffer can read, or write, or both.
667 *
668 * @c ios_base::in is automatically included in @a mode.
669 *
670 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
671 * to the base class initializer.
672 *
673 * That's a lie. We initialize the base class with NULL, because the
674 * string class does its own memory management.
675 */
676 explicit
677 basic_istringstream(const __string_type& __str,
678 ios_base::openmode __mode = ios_base::in)
679 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
680 { this->init(std::__addressof(_M_stringbuf)); }
681
682 /**
683 * @brief The destructor does nothing.
684 *
685 * The buffer is deallocated by the stringbuf object, not the
686 * formatting stream.
687 */
688 ~basic_istringstream()
689 { }
690
691#if __cplusplus >= 201103L
692 basic_istringstream(const basic_istringstream&) = delete;
693
694 basic_istringstream(basic_istringstream&& __rhs)
695 : __istream_type(std::move(__rhs)),
696 _M_stringbuf(std::move(__rhs._M_stringbuf))
697 { __istream_type::set_rdbuf(std::__addressof(_M_stringbuf)); }
698
699#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
700 // P0408 Efficient access to basic_stringbuf buffer
701 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
702 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
703 { this->init(std::__addressof(_M_stringbuf)); }
704
705 explicit
706 basic_istringstream(__string_type&& __str,
707 ios_base::openmode __mode = ios_base::in)
708 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
709 { this->init(std::__addressof(_M_stringbuf)); }
710
711 template<typename _SAlloc>
712 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
713 const allocator_type& __a)
714 : basic_istringstream(__str, ios_base::in, __a)
715 { }
716
717 template<typename _SAlloc>
718 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
719 ios_base::openmode __mode,
720 const allocator_type& __a)
721 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a)
722 { this->init(std::__addressof(_M_stringbuf)); }
723
724 template<typename _SAlloc>
725 explicit
726 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
727 ios_base::openmode __mode = ios_base::in)
728 : basic_istringstream(__str, __mode, allocator_type())
729 { }
730#endif // C++20
731
732#ifdef __cpp_lib_sstream_from_string_view
733 template <typename _Tp>
734 explicit
735 basic_istringstream(const _Tp& __t,
736 ios_base::openmode __mode = ios_base::in)
737 requires (is_convertible_v<const _Tp&,
738 basic_string_view<_CharT, _Traits>>)
739 : basic_istringstream(__t, __mode, allocator_type{})
740 { }
741
742 template <typename _Tp>
743 basic_istringstream(const _Tp& __t, const allocator_type& __a)
744 requires (is_convertible_v<const _Tp&,
745 basic_string_view<_CharT, _Traits>>)
746 : basic_istringstream(__t, ios_base::in, __a)
747 { }
748
749 template <typename _Tp>
750 basic_istringstream(const _Tp& __t, ios_base::openmode __mode,
751 const allocator_type& __a)
752 requires (is_convertible_v<const _Tp&,
753 basic_string_view<_CharT, _Traits>>)
754 : __istream_type(), _M_stringbuf(__t, __mode | ios_base::in, __a)
755 { this->init(std::__addressof(_M_stringbuf)); }
756#endif // C++26
757
758 // 27.8.3.2 Assign and swap:
759
760 basic_istringstream&
761 operator=(const basic_istringstream&) = delete;
762
763 basic_istringstream&
764 operator=(basic_istringstream&& __rhs)
765 {
766 __istream_type::operator=(std::move(__rhs));
767 _M_stringbuf = std::move(__rhs._M_stringbuf);
768 return *this;
769 }
770
771 void
772 swap(basic_istringstream& __rhs)
773 {
774 __istream_type::swap(__rhs);
775 _M_stringbuf.swap(__rhs._M_stringbuf);
776 }
777#endif // C++11
778
779 // Members:
780 /**
781 * @brief Accessing the underlying buffer.
782 * @return The current basic_stringbuf buffer.
783 *
784 * This hides both signatures of std::basic_ios::rdbuf().
785 */
786 _GLIBCXX_NODISCARD
787 __stringbuf_type*
788 rdbuf() const
789 { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); }
790
791 /**
792 * @brief Copying out the string buffer.
793 * @return @c rdbuf()->str()
794 */
795 _GLIBCXX_NODISCARD
796 __string_type
797 str() const _GLIBCXX_LVAL_REF_QUAL
798 { return _M_stringbuf.str(); }
799
800#if __cplusplus > 201703L
801#if _GLIBCXX_USE_CXX11_ABI
802#if __cpp_concepts
803 // P0407 Allocator-aware basic_streambuf
804 template<__allocator_like _SAlloc>
805 _GLIBCXX_NODISCARD
806 basic_string<_CharT, _Traits, _SAlloc>
807 str(const _SAlloc& __sa) const
808 { return _M_stringbuf.str(__sa); }
809#endif
810
811 _GLIBCXX_NODISCARD
812 __string_type
813 str() &&
814 { return std::move(_M_stringbuf).str(); }
815#endif // cxx11 ABI
816
817 _GLIBCXX_SSTREAM_ALWAYS_INLINE
818 basic_string_view<char_type, traits_type>
819 view() const noexcept
820 { return _M_stringbuf.view(); }
821#endif // C++20
822
823 /**
824 * @brief Setting a new buffer.
825 * @param __s The string to use as a new sequence.
826 *
827 * Calls @c rdbuf()->str(s).
828 */
829 void
830 str(const __string_type& __s)
831 { _M_stringbuf.str(__s); }
832
833#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
834#if __cpp_concepts
835 // P0407 Allocator-aware basic_streambuf
836 template<__allocator_like _SAlloc>
837 requires (!is_same_v<_SAlloc, _Alloc>)
838 void
839 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
840 { _M_stringbuf.str(__s); }
841#endif
842
843 void
844 str(__string_type&& __s)
845 { _M_stringbuf.str(std::move(__s)); }
846#endif
847
848#ifdef __cpp_lib_sstream_from_string_view
849 template<typename _Tp>
850 void
851 str(const _Tp& __t)
852 requires (is_convertible_v<const _Tp&,
853 basic_string_view<_CharT, _Traits>>)
854 { _M_stringbuf.str(__t); }
855#endif // C++26
856 };
857
858
859 // [27.7.3] Template class basic_ostringstream
860 /**
861 * @brief Controlling output for std::string.
862 * @ingroup io
863 *
864 * @tparam _CharT Type of character stream.
865 * @tparam _Traits Traits for character type, defaults to
866 * char_traits<_CharT>.
867 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
868 *
869 * This class supports writing to objects of type std::basic_string,
870 * using the inherited functions from std::basic_ostream. To control
871 * the associated sequence, an instance of std::basic_stringbuf is used,
872 * which this page refers to as @c sb.
873 */
874 template <typename _CharT, typename _Traits, typename _Alloc>
875 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
876 {
877 public:
878 // Types:
879 typedef _CharT char_type;
880 typedef _Traits traits_type;
881 // _GLIBCXX_RESOLVE_LIB_DEFECTS
882 // 251. basic_stringbuf missing allocator_type
883 typedef _Alloc allocator_type;
884 typedef typename traits_type::int_type int_type;
885 typedef typename traits_type::pos_type pos_type;
886 typedef typename traits_type::off_type off_type;
887
888 // Non-standard types:
889 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
890 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
891 typedef basic_ostream<char_type, traits_type> __ostream_type;
892
893 private:
894 __stringbuf_type _M_stringbuf;
895
896 public:
897 // Constructors/destructor:
898
899 /**
900 * @brief Default constructor starts with an empty string buffer.
901 *
902 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
903 * class initializer. Does not allocate any buffer.
904 *
905 * That's a lie. We initialize the base class with NULL, because the
906 * string class does its own memory management.
907 */
908 basic_ostringstream()
909 : __ostream_type(), _M_stringbuf(ios_base::out)
910 { this->init(std::__addressof(_M_stringbuf)); }
911
912 /**
913 * @brief Starts with an empty string buffer.
914 * @param __mode Whether the buffer can read, or write, or both.
915 *
916 * @c ios_base::out is automatically included in @a mode.
917 *
918 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
919 * class initializer. Does not allocate any buffer.
920 *
921 * That's a lie. We initialize the base class with NULL, because the
922 * string class does its own memory management.
923 */
924 explicit
925 basic_ostringstream(ios_base::openmode __mode)
926 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
927 { this->init(std::__addressof(_M_stringbuf)); }
928
929 /**
930 * @brief Starts with an existing string buffer.
931 * @param __str A string to copy as a starting buffer.
932 * @param __mode Whether the buffer can read, or write, or both.
933 *
934 * @c ios_base::out is automatically included in @a mode.
935 *
936 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
937 * to the base class initializer.
938 *
939 * That's a lie. We initialize the base class with NULL, because the
940 * string class does its own memory management.
941 */
942 explicit
943 basic_ostringstream(const __string_type& __str,
944 ios_base::openmode __mode = ios_base::out)
945 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
946 { this->init(std::__addressof(_M_stringbuf)); }
947
948 /**
949 * @brief The destructor does nothing.
950 *
951 * The buffer is deallocated by the stringbuf object, not the
952 * formatting stream.
953 */
954 ~basic_ostringstream()
955 { }
956
957#if __cplusplus >= 201103L
958 basic_ostringstream(const basic_ostringstream&) = delete;
959
960 basic_ostringstream(basic_ostringstream&& __rhs)
961 : __ostream_type(std::move(__rhs)),
962 _M_stringbuf(std::move(__rhs._M_stringbuf))
963 { __ostream_type::set_rdbuf(std::__addressof(_M_stringbuf)); }
964
965#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
966 // P0408 Efficient access to basic_stringbuf buffer
967 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
968 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
969 { this->init(std::__addressof(_M_stringbuf)); }
970
971 explicit
972 basic_ostringstream(__string_type&& __str,
973 ios_base::openmode __mode = ios_base::out)
974 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
975 { this->init(std::__addressof(_M_stringbuf)); }
976
977 template<typename _SAlloc>
978 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
979 const allocator_type& __a)
980 : basic_ostringstream(__str, ios_base::out, __a)
981 { }
982
983 template<typename _SAlloc>
984 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
985 ios_base::openmode __mode,
986 const allocator_type& __a)
987 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a)
988 { this->init(std::__addressof(_M_stringbuf)); }
989
990 template<typename _SAlloc>
991 explicit
992 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
993 ios_base::openmode __mode = ios_base::out)
994 : basic_ostringstream(__str, __mode, allocator_type())
995 { }
996#endif // C++20
997
998#ifdef __cpp_lib_sstream_from_string_view
999 template <typename _Tp>
1000 explicit
1001 basic_ostringstream(
1002 const _Tp& __t, ios_base::openmode __mode = ios_base::out)
1003 requires (is_convertible_v<const _Tp&,
1004 basic_string_view<_CharT, _Traits>>)
1005 : basic_ostringstream(__t, __mode, allocator_type{})
1006 { }
1007
1008 template <typename _Tp>
1009 basic_ostringstream(const _Tp& __t, const allocator_type& __a)
1010 requires (is_convertible_v<const _Tp&,
1011 basic_string_view<_CharT, _Traits>>)
1012 : basic_ostringstream(__t, ios_base::out, __a)
1013 { }
1014
1015 template <typename _Tp>
1016 basic_ostringstream(const _Tp& __t, ios_base::openmode __mode,
1017 const allocator_type& __a)
1018 requires (is_convertible_v<const _Tp&,
1019 basic_string_view<_CharT, _Traits>>)
1020 : __ostream_type(), _M_stringbuf(__t, __mode | ios_base::out, __a)
1021 { this->init(std::__addressof(_M_stringbuf)); }
1022#endif // C++26
1023
1024 // 27.8.3.2 Assign and swap:
1025
1026 basic_ostringstream&
1027 operator=(const basic_ostringstream&) = delete;
1028
1029 basic_ostringstream&
1030 operator=(basic_ostringstream&& __rhs)
1031 {
1032 __ostream_type::operator=(std::move(__rhs));
1033 _M_stringbuf = std::move(__rhs._M_stringbuf);
1034 return *this;
1035 }
1036
1037 void
1038 swap(basic_ostringstream& __rhs)
1039 {
1040 __ostream_type::swap(__rhs);
1041 _M_stringbuf.swap(__rhs._M_stringbuf);
1042 }
1043#endif // C++11
1044
1045 // Members:
1046 /**
1047 * @brief Accessing the underlying buffer.
1048 * @return The current basic_stringbuf buffer.
1049 *
1050 * This hides both signatures of std::basic_ios::rdbuf().
1051 */
1052 _GLIBCXX_NODISCARD
1053 __stringbuf_type*
1054 rdbuf() const
1055 { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); }
1056
1057 /**
1058 * @brief Copying out the string buffer.
1059 * @return @c rdbuf()->str()
1060 */
1061 _GLIBCXX_NODISCARD
1062 __string_type
1063 str() const _GLIBCXX_LVAL_REF_QUAL
1064 { return _M_stringbuf.str(); }
1065
1066#if __cplusplus > 201703L
1067#if _GLIBCXX_USE_CXX11_ABI
1068#if __cpp_concepts
1069 // P0407 Allocator-aware basic_streambuf
1070 template<__allocator_like _SAlloc>
1071 _GLIBCXX_NODISCARD
1072 basic_string<_CharT, _Traits, _SAlloc>
1073 str(const _SAlloc& __sa) const
1074 { return _M_stringbuf.str(__sa); }
1075#endif
1076
1077 _GLIBCXX_NODISCARD
1078 __string_type
1079 str() &&
1080 { return std::move(_M_stringbuf).str(); }
1081#endif // cxx11 ABI
1082
1083 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1084 basic_string_view<char_type, traits_type>
1085 view() const noexcept
1086 { return _M_stringbuf.view(); }
1087#endif // C++20
1088
1089 /**
1090 * @brief Setting a new buffer.
1091 * @param __s The string to use as a new sequence.
1092 *
1093 * Calls @c rdbuf()->str(s).
1094 */
1095 void
1096 str(const __string_type& __s)
1097 { _M_stringbuf.str(__s); }
1098
1099#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1100#if __cpp_concepts
1101 // P0407 Allocator-aware basic_streambuf
1102 template<__allocator_like _SAlloc>
1103 requires (!is_same_v<_SAlloc, _Alloc>)
1104 void
1105 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1106 { _M_stringbuf.str(__s); }
1107#endif
1108
1109 void
1110 str(__string_type&& __s)
1111 { _M_stringbuf.str(std::move(__s)); }
1112#endif
1113
1114#ifdef __cpp_lib_sstream_from_string_view
1115 template<typename _Tp>
1116 void
1117 str(const _Tp& __t)
1118 requires (is_convertible_v<const _Tp&,
1119 basic_string_view<_CharT, _Traits>>)
1120 { _M_stringbuf.str(__t); }
1121#endif // C++26
1122 };
1123
1124
1125 // [27.7.4] Template class basic_stringstream
1126 /**
1127 * @brief Controlling input and output for std::string.
1128 * @ingroup io
1129 *
1130 * @tparam _CharT Type of character stream.
1131 * @tparam _Traits Traits for character type, defaults to
1132 * char_traits<_CharT>.
1133 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
1134 *
1135 * This class supports reading from and writing to objects of type
1136 * std::basic_string, using the inherited functions from
1137 * std::basic_iostream. To control the associated sequence, an instance
1138 * of std::basic_stringbuf is used, which this page refers to as @c sb.
1139 */
1140 template <typename _CharT, typename _Traits, typename _Alloc>
1141 class basic_stringstream : public basic_iostream<_CharT, _Traits>
1142 {
1143 public:
1144 // Types:
1145 typedef _CharT char_type;
1146 typedef _Traits traits_type;
1147 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1148 // 251. basic_stringbuf missing allocator_type
1149 typedef _Alloc allocator_type;
1150 typedef typename traits_type::int_type int_type;
1151 typedef typename traits_type::pos_type pos_type;
1152 typedef typename traits_type::off_type off_type;
1153
1154 // Non-standard Types:
1155 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1156 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
1157 typedef basic_iostream<char_type, traits_type> __iostream_type;
1158
1159 private:
1160 __stringbuf_type _M_stringbuf;
1161
1162 public:
1163 // Constructors/destructors
1164
1165 /**
1166 * @brief Default constructor starts with an empty string buffer.
1167 *
1168 * Initializes @c sb using the mode @c in|out, and passes @c &sb
1169 * to the base class initializer. Does not allocate any buffer.
1170 *
1171 * That's a lie. We initialize the base class with NULL, because the
1172 * string class does its own memory management.
1173 */
1174 basic_stringstream()
1175 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1176 { this->init(std::__addressof(_M_stringbuf)); }
1177
1178 /**
1179 * @brief Starts with an empty string buffer.
1180 * @param __m Whether the buffer can read, or write, or both.
1181 *
1182 * Initializes @c sb using the mode from @c __m, and passes @c &sb
1183 * to the base class initializer. Does not allocate any buffer.
1184 *
1185 * That's a lie. We initialize the base class with NULL, because the
1186 * string class does its own memory management.
1187 */
1188 explicit
1189 basic_stringstream(ios_base::openmode __m)
1190 : __iostream_type(), _M_stringbuf(__m)
1191 { this->init(std::__addressof(_M_stringbuf)); }
1192
1193 /**
1194 * @brief Starts with an existing string buffer.
1195 * @param __str A string to copy as a starting buffer.
1196 * @param __m Whether the buffer can read, or write, or both.
1197 *
1198 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1199 * to the base class initializer.
1200 *
1201 * That's a lie. We initialize the base class with NULL, because the
1202 * string class does its own memory management.
1203 */
1204 explicit
1205 basic_stringstream(const __string_type& __str,
1206 ios_base::openmode __m = ios_base::out | ios_base::in)
1207 : __iostream_type(), _M_stringbuf(__str, __m)
1208 { this->init(std::__addressof(_M_stringbuf)); }
1209
1210 /**
1211 * @brief The destructor does nothing.
1212 *
1213 * The buffer is deallocated by the stringbuf object, not the
1214 * formatting stream.
1215 */
1216 ~basic_stringstream()
1217 { }
1218
1219#if __cplusplus >= 201103L
1220 basic_stringstream(const basic_stringstream&) = delete;
1221
1222 basic_stringstream(basic_stringstream&& __rhs)
1223 : __iostream_type(std::move(__rhs)),
1224 _M_stringbuf(std::move(__rhs._M_stringbuf))
1225 { __iostream_type::set_rdbuf(std::__addressof(_M_stringbuf)); }
1226
1227#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1228 // P0408 Efficient access to basic_stringbuf buffer
1229 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
1230 : __iostream_type(), _M_stringbuf(__mode, __a)
1231 { this->init(std::__addressof(_M_stringbuf)); }
1232
1233 explicit
1234 basic_stringstream(__string_type&& __str,
1235 ios_base::openmode __mode = ios_base::in
1236 | ios_base::out)
1237 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1238 { this->init(std::__addressof(_M_stringbuf)); }
1239
1240 template<typename _SAlloc>
1241 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1242 const allocator_type& __a)
1243 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
1244 { }
1245
1246 template<typename _SAlloc>
1247 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1248 ios_base::openmode __mode,
1249 const allocator_type& __a)
1250 : __iostream_type(), _M_stringbuf(__str, __mode, __a)
1251 { this->init(std::__addressof(_M_stringbuf)); }
1252
1253 template<typename _SAlloc>
1254 explicit
1255 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1256 ios_base::openmode __mode = ios_base::in
1257 | ios_base::out)
1258 : basic_stringstream(__str, __mode, allocator_type())
1259 { }
1260#endif // C++20
1261
1262#ifdef __cpp_lib_sstream_from_string_view
1263 template <typename _Tp>
1264 explicit
1265 basic_stringstream(const _Tp& __t,
1266 ios_base::openmode __mode = ios_base::in | ios_base::out)
1267 requires (is_convertible_v<const _Tp&,
1268 basic_string_view<_CharT, _Traits>>)
1269 : basic_stringstream(__t, __mode, allocator_type{})
1270 { }
1271
1272 template <typename _Tp>
1273 basic_stringstream(const _Tp& __t, const allocator_type& __a)
1274 requires (is_convertible_v<const _Tp&,
1275 basic_string_view<_CharT, _Traits>>)
1276 : basic_stringstream(__t, ios_base::in | ios_base::out, __a)
1277 { }
1278
1279 template <typename _Tp>
1280 basic_stringstream(const _Tp& __t, ios_base::openmode __mode,
1281 const allocator_type& __a)
1282 requires (is_convertible_v<const _Tp&, basic_string_view<_CharT, _Traits>>)
1283 : __iostream_type(), _M_stringbuf(__t, __mode, __a)
1284 { this->init(std::__addressof(_M_stringbuf)); }
1285#endif // C++26
1286
1287 // 27.8.3.2 Assign and swap:
1288
1289 basic_stringstream&
1290 operator=(const basic_stringstream&) = delete;
1291
1292 basic_stringstream&
1293 operator=(basic_stringstream&& __rhs)
1294 {
1295 __iostream_type::operator=(std::move(__rhs));
1296 _M_stringbuf = std::move(__rhs._M_stringbuf);
1297 return *this;
1298 }
1299
1300 void
1301 swap(basic_stringstream& __rhs)
1302 {
1303 __iostream_type::swap(__rhs);
1304 _M_stringbuf.swap(__rhs._M_stringbuf);
1305 }
1306#endif // C++11
1307
1308 // Members:
1309 /**
1310 * @brief Accessing the underlying buffer.
1311 * @return The current basic_stringbuf buffer.
1312 *
1313 * This hides both signatures of std::basic_ios::rdbuf().
1314 */
1315 _GLIBCXX_NODISCARD
1316 __stringbuf_type*
1317 rdbuf() const
1318 { return const_cast<__stringbuf_type*>(std::__addressof(_M_stringbuf)); }
1319
1320 /**
1321 * @brief Copying out the string buffer.
1322 * @return @c rdbuf()->str()
1323 */
1324 _GLIBCXX_NODISCARD
1325 __string_type
1326 str() const _GLIBCXX_LVAL_REF_QUAL
1327 { return _M_stringbuf.str(); }
1328
1329#if __cplusplus > 201703L
1330#if _GLIBCXX_USE_CXX11_ABI
1331#if __cpp_concepts
1332 // P0407 Allocator-aware basic_streambuf
1333 template<__allocator_like _SAlloc>
1334 _GLIBCXX_NODISCARD
1335 basic_string<_CharT, _Traits, _SAlloc>
1336 str(const _SAlloc& __sa) const
1337 { return _M_stringbuf.str(__sa); }
1338#endif
1339
1340 _GLIBCXX_NODISCARD
1341 __string_type
1342 str() &&
1343 { return std::move(_M_stringbuf).str(); }
1344#endif // cxx11 ABI
1345
1346 _GLIBCXX_SSTREAM_ALWAYS_INLINE
1347 basic_string_view<char_type, traits_type>
1348 view() const noexcept
1349 { return _M_stringbuf.view(); }
1350#endif // C++20
1351
1352 /**
1353 * @brief Setting a new buffer.
1354 * @param __s The string to use as a new sequence.
1355 *
1356 * Calls @c rdbuf()->str(s).
1357 */
1358 void
1359 str(const __string_type& __s)
1360 { _M_stringbuf.str(__s); }
1361
1362#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1363#if __cpp_concepts
1364 // P0407 Allocator-aware basic_streambuf
1365 template<__allocator_like _SAlloc>
1366 requires (!is_same_v<_SAlloc, _Alloc>)
1367 void
1368 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1369 { _M_stringbuf.str(__s); }
1370#endif
1371
1372 void
1373 str(__string_type&& __s)
1374 { _M_stringbuf.str(std::move(__s)); }
1375#endif
1376
1377#ifdef __cpp_lib_sstream_from_string_view
1378 template<typename _Tp>
1379 void
1380 str(const _Tp& __t)
1381 requires (is_convertible_v<const _Tp&,
1382 basic_string_view<_CharT, _Traits>>)
1383 { _M_stringbuf.str(__t); }
1384#endif // C++26
1385 };
1386
1387#if __cplusplus >= 201103L
1388 /// Swap specialization for stringbufs.
1389 template <class _CharT, class _Traits, class _Allocator>
1390 inline void
1391 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1392 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1393 noexcept(noexcept(__x.swap(__y)))
1394 { __x.swap(__y); }
1395
1396 /// Swap specialization for istringstreams.
1397 template <class _CharT, class _Traits, class _Allocator>
1398 inline void
1399 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1400 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1401 { __x.swap(__y); }
1402
1403 /// Swap specialization for ostringstreams.
1404 template <class _CharT, class _Traits, class _Allocator>
1405 inline void
1406 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1407 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1408 { __x.swap(__y); }
1409
1410 /// Swap specialization for stringstreams.
1411 template <class _CharT, class _Traits, class _Allocator>
1412 inline void
1413 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1414 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1415 { __x.swap(__y); }
1416#endif // C++11
1417
1418_GLIBCXX_END_NAMESPACE_CXX11
1419_GLIBCXX_END_NAMESPACE_VERSION
1420} // namespace
1421
1422#undef _GLIBCXX_SSTREAM_ALWAYS_INLINE
1423#undef _GLIBCXX_LVAL_REF_QUAL
1424
1425#include <bits/sstream.tcc>
1426
1427#endif /* _GLIBCXX_SSTREAM */
istream
ostream
string_view
iosfwd_string.h
version.h
requires_hosted.h
sstream.tcc
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
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::size
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition range_access.h:294
std::data
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition range_access.h:354
__gnu_debug::__base
constexpr _Iterator __base(_Iterator __it)
Definition helper_functions.h:339
std::basic_istream::basic_istream
basic_istream(__streambuf_type *__sb)
Base constructor.
Definition istream:107
std::basic_iostream::basic_iostream
basic_iostream(basic_streambuf< _CharT, _Traits > *__sb)
Constructor does nothing.
Definition istream:1027
std::basic_stringbuf
The actual work of input and output (for std::string).
Definition sstream:89
std::basic_stringbuf::showmanyc
virtual streamsize showmanyc()
Investigating the data available.
Definition sstream:405
std::basic_stringbuf::underflow
virtual int_type underflow()
Fetches more data from the controlled sequence.
Definition sstream.tcc:154
std::basic_stringbuf::seekpos
virtual pos_type seekpos(pos_type __sp, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition sstream.tcc:220
std::basic_stringbuf::seekoff
virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition sstream.tcc:172
std::basic_stringbuf::overflow
virtual int_type overflow(int_type __c=traits_type::eof())
Consumes data from the buffer; writes to the controlled sequence.
Definition sstream.tcc:84
std::basic_stringbuf::basic_stringbuf
basic_stringbuf()
Starts with an empty string buffer.
Definition sstream:129
std::basic_stringbuf::pbackfail
virtual int_type pbackfail(int_type __c=traits_type::eof())
Tries to back up the input sequence.
Definition sstream.tcc:50
std::basic_stringbuf::str
__string_type str() const &
Copying out the string buffer.
Definition sstream:288
std::basic_stringbuf::setbuf
virtual __streambuf_type * setbuf(char_type *__s, streamsize __n)
Manipulates the buffer.
Definition sstream:437
std::basic_stringbuf< char >::_M_mode
ios_base::openmode _M_mode
Definition sstream:115
std::basic_istringstream
Controlling input for std::string.
Definition sstream:610
std::basic_istringstream::str
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:830
std::basic_istringstream::rdbuf
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:788
std::basic_istringstream::~basic_istringstream
~basic_istringstream()
The destructor does nothing.
Definition sstream:688
std::basic_istringstream::basic_istringstream
basic_istringstream(const __string_type &__str, ios_base::openmode __mode=ios_base::in)
Starts with an existing string buffer.
Definition sstream:677
std::basic_istringstream::str
__string_type str() const &
Copying out the string buffer.
Definition sstream:797
std::basic_istringstream::basic_istringstream
basic_istringstream()
Default constructor starts with an empty string buffer.
Definition sstream:642
std::basic_istringstream::basic_istringstream
basic_istringstream(ios_base::openmode __mode)
Starts with an empty string buffer.
Definition sstream:659
std::basic_ostringstream
Controlling output for std::string.
Definition sstream:876
std::basic_ostringstream::~basic_ostringstream
~basic_ostringstream()
The destructor does nothing.
Definition sstream:954
std::basic_ostringstream::str
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:1096
std::basic_ostringstream::basic_ostringstream
basic_ostringstream()
Default constructor starts with an empty string buffer.
Definition sstream:908
std::basic_ostringstream::basic_ostringstream
basic_ostringstream(const __string_type &__str, ios_base::openmode __mode=ios_base::out)
Starts with an existing string buffer.
Definition sstream:943
std::basic_ostringstream::str
__string_type str() const &
Copying out the string buffer.
Definition sstream:1063
std::basic_ostringstream::basic_ostringstream
basic_ostringstream(ios_base::openmode __mode)
Starts with an empty string buffer.
Definition sstream:925
std::basic_ostringstream::rdbuf
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:1054
std::basic_stringstream
Controlling input and output for std::string.
Definition sstream:1142
std::basic_stringstream::~basic_stringstream
~basic_stringstream()
The destructor does nothing.
Definition sstream:1216
std::basic_stringstream::basic_stringstream
basic_stringstream(const __string_type &__str, ios_base::openmode __m=ios_base::out|ios_base::in)
Starts with an existing string buffer.
Definition sstream:1205
std::basic_stringstream::basic_stringstream
basic_stringstream()
Default constructor starts with an empty string buffer.
Definition sstream:1174
std::basic_stringstream::str
void str(const __string_type &__s)
Setting a new buffer.
Definition sstream:1359
std::basic_stringstream::str
__string_type str() const &
Copying out the string buffer.
Definition sstream:1326
std::basic_stringstream::rdbuf
__stringbuf_type * rdbuf() const
Accessing the underlying buffer.
Definition sstream:1317
std::basic_stringstream::basic_stringstream
basic_stringstream(ios_base::openmode __m)
Starts with an empty string buffer.
Definition sstream:1189
std::basic_streambuf::pptr
char_type * pptr() const
Access to the put area.
Definition streambuf:541
std::basic_streambuf::char_type
_CharT char_type
Definition streambuf:135
std::basic_streambuf::setg
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
Definition streambuf:518
std::basic_streambuf::eback
char_type * eback() const
Access to the get area.
Definition streambuf:491
std::basic_streambuf::egptr
char_type * egptr() const
Access to the get area.
Definition streambuf:497
std::basic_streambuf::gptr
char_type * gptr() const
Access to the get area.
Definition streambuf:494
std::basic_streambuf::pubimbue
locale pubimbue(const locale &__loc)
Entry point for imbue().
Definition streambuf:218
std::basic_streambuf::pbase
char_type * pbase() const
Access to the put area.
Definition streambuf:538
std::basic_streambuf::off_type
traits_type::off_type off_type
Definition streambuf:139
std::basic_streambuf::basic_streambuf
basic_streambuf()
Base constructor.
Definition streambuf:472
std::allocator_traits
Uniform interface to all allocator types.
Definition bits/alloc_traits.h:250
std::allocator_traits::is_always_equal
typename __detected_or_t< is_empty< _Alloc >, __equal, _Alloc >::type is_always_equal
Whether all instances of the allocator type compare equal.
Definition bits/alloc_traits.h:365
std::allocator_traits::propagate_on_container_swap
__detected_or_t< false_type, __pocs, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
Definition bits/alloc_traits.h:357
std::basic_ios::init
void init(basic_streambuf< _CharT, _Traits > *__sb)
All setup is performed here.
Definition basic_ios.tcc:131
std::basic_string
Managing sequences of characters and character-like objects.
Definition basic_string.h:94
std::basic_string::size
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition basic_string.h:1217
std::basic_string::data
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
Definition basic_string.h:2968
std::basic_string::assign
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
Definition basic_string.h:1808
std::ios_base
The base of the I/O class hierarchy.
Definition ios_base.h:266
std::ios_base::in
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:498
std::ios_base::out
static const openmode out
Open for output. Default for ofstream and fstream.
Definition ios_base.h:501
std::ios_base::openmode
_Ios_Openmode openmode
This is a bitmask type.
Definition ios_base.h:484
std::ios_base::app
static const openmode app
Seek to end before each write.
Definition ios_base.h:487
std::ios_base::seekdir
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:523
std::ios_base::ate
static const openmode ate
Open and seek to end immediately after opening.
Definition ios_base.h:490
std::basic_ostream::basic_ostream
basic_ostream(__streambuf_type *__sb)
Base constructor.
Definition ostream.h:97
alloc_traits.h