libstdc++: iomanip Source File

libstdc++

iomanip
Go to the documentation of this file.
1// Standard stream manipulators -*- 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/iomanip
26 * This is a Standard C++ Library header.
27 */
28
29//
30// ISO C++ 14882: 27.6.3 Standard manipulators
31//
32
33#ifndef _GLIBCXX_IOMANIP
34#define _GLIBCXX_IOMANIP 1
35
36#ifdef _GLIBCXX_SYSHDR
37#pragma GCC system_header
38#endif
39
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Wc++11-extensions"
42
43#include <bits/requires_hosted.h> // iostreams
44
45#include <bits/c++config.h>
46#include <iosfwd>
47#include <bits/ios_base.h>
48
49#define __glibcxx_want_quoted_string_io
50#include <bits/version.h>
51
52#if __cplusplus >= 201103L
53#include <locale>
54#if __cplusplus > 201103L
55#include <bits/quoted_string.h>
56#endif
57#endif
58
59namespace std _GLIBCXX_VISIBILITY(default)
60{
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
62
63 // [27.6.3] standard manipulators
64 // Also see DR 183.
65
66 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
67
68 /**
69 * @brief Manipulator for @c setf.
70 * @param __mask A format flags mask.
71 *
72 * Sent to a stream object, this manipulator resets the specified flags,
73 * via @e stream.setf(0,__mask).
74 */
75 inline _Resetiosflags
76 resetiosflags(ios_base::fmtflags __mask)
77 { return { __mask }; }
78
79 template<typename _CharT, typename _Traits>
80 inline basic_istream<_CharT, _Traits>&
81 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
82 {
83 __is.setf(ios_base::fmtflags(0), __f._M_mask);
84 return __is;
85 }
86
87 template<typename _CharT, typename _Traits>
88 inline basic_ostream<_CharT, _Traits>&
89 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
90 {
91 __os.setf(ios_base::fmtflags(0), __f._M_mask);
92 return __os;
93 }
94
95
96 struct _Setiosflags { ios_base::fmtflags _M_mask; };
97
98 /**
99 * @brief Manipulator for @c setf.
100 * @param __mask A format flags mask.
101 *
102 * Sent to a stream object, this manipulator sets the format flags
103 * to @a __mask.
104 */
105 inline _Setiosflags
106 setiosflags(ios_base::fmtflags __mask)
107 { return { __mask }; }
108
109 template<typename _CharT, typename _Traits>
110 inline basic_istream<_CharT, _Traits>&
111 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
112 {
113 __is.setf(__f._M_mask);
114 return __is;
115 }
116
117 template<typename _CharT, typename _Traits>
118 inline basic_ostream<_CharT, _Traits>&
119 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
120 {
121 __os.setf(__f._M_mask);
122 return __os;
123 }
124
125
126 struct _Setbase { int _M_base; };
127
128 /**
129 * @brief Manipulator for @c setf.
130 * @param __base A numeric base.
131 *
132 * Sent to a stream object, this manipulator changes the
133 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
134 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
135 */
136 inline _Setbase
137 setbase(int __base)
138 { return { __base }; }
139
140 template<typename _CharT, typename _Traits>
141 inline basic_istream<_CharT, _Traits>&
142 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
143 {
144 __is.setf(__f._M_base == 8 ? ios_base::oct :
145 __f._M_base == 10 ? ios_base::dec :
146 __f._M_base == 16 ? ios_base::hex :
147 ios_base::fmtflags(0), ios_base::basefield);
148 return __is;
149 }
150
151 template<typename _CharT, typename _Traits>
152 inline basic_ostream<_CharT, _Traits>&
153 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
154 {
155 __os.setf(__f._M_base == 8 ? ios_base::oct :
156 __f._M_base == 10 ? ios_base::dec :
157 __f._M_base == 16 ? ios_base::hex :
158 ios_base::fmtflags(0), ios_base::basefield);
159 return __os;
160 }
161
162
163 template<typename _CharT>
164 struct _Setfill { _CharT _M_c; };
165
166 /**
167 * @brief Manipulator for @c fill.
168 * @param __c The new fill character.
169 *
170 * Sent to a stream object, this manipulator calls @c fill(__c) for that
171 * object.
172 */
173 template<typename _CharT>
174 inline _Setfill<_CharT>
175 setfill(_CharT __c)
176 { return { __c }; }
177
178 template<typename _CharT, typename _Traits>
179 __attribute__((__deprecated__("'std::setfill' should only be used with "
180 "output streams")))
181 inline basic_istream<_CharT, _Traits>&
182 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
183 {
184 __is.fill(__f._M_c);
185 return __is;
186 }
187
188 template<typename _CharT, typename _Traits>
189 inline basic_ostream<_CharT, _Traits>&
190 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
191 {
192 __os.fill(__f._M_c);
193 return __os;
194 }
195
196
197 struct _Setprecision { int _M_n; };
198
199 /**
200 * @brief Manipulator for @c precision.
201 * @param __n The new precision.
202 *
203 * Sent to a stream object, this manipulator calls @c precision(__n) for
204 * that object.
205 */
206 inline _Setprecision
207 setprecision(int __n)
208 { return { __n }; }
209
210 template<typename _CharT, typename _Traits>
211 inline basic_istream<_CharT, _Traits>&
212 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
213 {
214 __is.precision(__f._M_n);
215 return __is;
216 }
217
218 template<typename _CharT, typename _Traits>
219 inline basic_ostream<_CharT, _Traits>&
220 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
221 {
222 __os.precision(__f._M_n);
223 return __os;
224 }
225
226
227 struct _Setw { int _M_n; };
228
229 /**
230 * @brief Manipulator for @c width.
231 * @param __n The new width.
232 *
233 * Sent to a stream object, this manipulator calls @c width(__n) for
234 * that object.
235 */
236 inline _Setw
237 setw(int __n)
238 { return { __n }; }
239
240 template<typename _CharT, typename _Traits>
241 inline basic_istream<_CharT, _Traits>&
242 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
243 {
244 __is.width(__f._M_n);
245 return __is;
246 }
247
248 template<typename _CharT, typename _Traits>
249 inline basic_ostream<_CharT, _Traits>&
250 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
251 {
252 __os.width(__f._M_n);
253 return __os;
254 }
255
256#if __cplusplus >= 201103L
257
258 template<typename _MoneyT>
259 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
260
261 /**
262 * @brief Extended manipulator for extracting money.
263 * @param __mon Either long double or a specialization of @c basic_string.
264 * @param __intl A bool indicating whether international format
265 * is to be used.
266 *
267 * Sent to a stream object, this manipulator extracts @a __mon.
268 */
269 template<typename _MoneyT>
270 inline _Get_money<_MoneyT>
271 get_money(_MoneyT& __mon, bool __intl = false)
272 { return { __mon, __intl }; }
273
274 template<typename _CharT, typename _Traits, typename _MoneyT>
275 basic_istream<_CharT, _Traits>&
276 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
277 {
278 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
279 if (__cerb)
280 {
281 ios_base::iostate __err = ios_base::goodbit;
282 __try
283 {
284 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
285 typedef money_get<_CharT, _Iter> _MoneyGet;
286
287 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
288 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
289 __is, __err, __f._M_mon);
290 }
291 __catch(__cxxabiv1::__forced_unwind&)
292 {
293 __is._M_setstate(ios_base::badbit);
294 __throw_exception_again;
295 }
296 __catch(...)
297 { __is._M_setstate(ios_base::badbit); }
298 if (__err)
299 __is.setstate(__err);
300 }
301 return __is;
302 }
303
304
305 template<typename _MoneyT>
306 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
307
308 /**
309 * @brief Extended manipulator for inserting money.
310 * @param __mon Either long double or a specialization of @c basic_string.
311 * @param __intl A bool indicating whether international format
312 * is to be used.
313 *
314 * Sent to a stream object, this manipulator inserts @a __mon.
315 */
316 template<typename _MoneyT>
317 inline _Put_money<_MoneyT>
318 put_money(const _MoneyT& __mon, bool __intl = false)
319 { return { __mon, __intl }; }
320
321 template<typename _CharT, typename _Traits, typename _MoneyT>
322 basic_ostream<_CharT, _Traits>&
323 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
324 {
325 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
326 if (__cerb)
327 {
328 ios_base::iostate __err = ios_base::goodbit;
329 __try
330 {
331 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
332 typedef money_put<_CharT, _Iter> _MoneyPut;
333
334 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
335 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
336 __os.fill(), __f._M_mon).failed())
337 __err |= ios_base::badbit;
338 }
339 __catch(__cxxabiv1::__forced_unwind&)
340 {
341 __os._M_setstate(ios_base::badbit);
342 __throw_exception_again;
343 }
344 __catch(...)
345 { __os._M_setstate(ios_base::badbit); }
346 if (__err)
347 __os.setstate(__err);
348 }
349 return __os;
350 }
351
352 template<typename _CharT>
353 struct _Put_time
354 {
355 const std::tm* _M_tmb;
356 const _CharT* _M_fmt;
357 };
358
359 /**
360 * @brief Extended manipulator for formatting time.
361 *
362 * This manipulator uses time_put::put to format time.
363 * [ext.manip]
364 *
365 * @param __tmb struct tm time data to format.
366 * @param __fmt format string.
367 */
368 template<typename _CharT>
369 inline _Put_time<_CharT>
370 put_time(const std::tm* __tmb, const _CharT* __fmt)
371 { return { __tmb, __fmt }; }
372
373 template<typename _CharT, typename _Traits>
374 basic_ostream<_CharT, _Traits>&
375 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
376 {
377 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
378 if (__cerb)
379 {
380 ios_base::iostate __err = ios_base::goodbit;
381 __try
382 {
383 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
384 typedef time_put<_CharT, _Iter> _TimePut;
385
386 const _CharT* const __fmt_end = __f._M_fmt +
387 _Traits::length(__f._M_fmt);
388
389 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
390 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
391 __f._M_tmb, __f._M_fmt, __fmt_end).failed())
392 __err |= ios_base::badbit;
393 }
394 __catch(__cxxabiv1::__forced_unwind&)
395 {
396 __os._M_setstate(ios_base::badbit);
397 __throw_exception_again;
398 }
399 __catch(...)
400 { __os._M_setstate(ios_base::badbit); }
401 if (__err)
402 __os.setstate(__err);
403 }
404 return __os;
405 }
406
407 template<typename _CharT>
408 struct _Get_time
409 {
410 std::tm* _M_tmb;
411 const _CharT* _M_fmt;
412 };
413
414 /**
415 * @brief Extended manipulator for extracting time.
416 *
417 * This manipulator uses time_get::get to extract time.
418 * [ext.manip]
419 *
420 * @param __tmb struct to extract the time data to.
421 * @param __fmt format string.
422 */
423 template<typename _CharT>
424 inline _Get_time<_CharT>
425 get_time(std::tm* __tmb, const _CharT* __fmt)
426 { return { __tmb, __fmt }; }
427
428 template<typename _CharT, typename _Traits>
429 basic_istream<_CharT, _Traits>&
430 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
431 {
432 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
433 if (__cerb)
434 {
435 ios_base::iostate __err = ios_base::goodbit;
436 __try
437 {
438 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
439 typedef time_get<_CharT, _Iter> _TimeGet;
440
441 const _CharT* const __fmt_end = __f._M_fmt +
442 _Traits::length(__f._M_fmt);
443
444 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
445 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
446 __err, __f._M_tmb, __f._M_fmt, __fmt_end);
447 }
448 __catch(__cxxabiv1::__forced_unwind&)
449 {
450 __is._M_setstate(ios_base::badbit);
451 __throw_exception_again;
452 }
453 __catch(...)
454 { __is._M_setstate(ios_base::badbit); }
455 if (__err)
456 __is.setstate(__err);
457 }
458 return __is;
459 }
460
461#ifdef __cpp_lib_quoted_string_io // C++ >= 14 && HOSTED
462
463 /**
464 * @brief Manipulator for quoted strings.
465 * @param __string String to quote.
466 * @param __delim Character to quote string with.
467 * @param __escape Escape character to escape itself or quote character.
468 * @since C++14
469 */
470 template<typename _CharT>
471 inline auto
472 quoted(const _CharT* __string,
473 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
474 {
475 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
476 __escape);
477 }
478
479 template<typename _CharT, typename _Traits, typename _Alloc>
480 inline auto
481 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
482 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
483 {
484 return __detail::_Quoted_string<
485 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
486 __string, __delim, __escape);
487 }
488
489 template<typename _CharT, typename _Traits, typename _Alloc>
490 inline auto
491 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
492 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
493 {
494 return __detail::_Quoted_string<
495 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
496 __string, __delim, __escape);
497 }
498
499#if __cplusplus >= 201703L
500 // _GLIBCXX_RESOLVE_LIB_DEFECTS
501 // 2785. quoted should work with basic_string_view
502 template<typename _CharT, typename _Traits>
503 inline auto
504 quoted(basic_string_view<_CharT, _Traits> __sv,
505 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
506 {
507 return __detail::_Quoted_string<
508 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
509 }
510#endif // C++17
511#endif // __cpp_lib_quoted_string_io
512
513#endif // __cplusplus >= 201103L
514
515 // Inhibit implicit instantiations for required instantiations,
516 // which are defined via explicit instantiations elsewhere.
517 // NB: This syntax is a GNU extension.
518#if _GLIBCXX_EXTERN_TEMPLATE
519 extern template ostream& operator<<(ostream&, _Setfill<char>);
520 extern template ostream& operator<<(ostream&, _Setiosflags);
521 extern template ostream& operator<<(ostream&, _Resetiosflags);
522 extern template ostream& operator<<(ostream&, _Setbase);
523 extern template ostream& operator<<(ostream&, _Setprecision);
524 extern template ostream& operator<<(ostream&, _Setw);
525 extern template istream& operator>>(istream&, _Setfill<char>);
526 extern template istream& operator>>(istream&, _Setiosflags);
527 extern template istream& operator>>(istream&, _Resetiosflags);
528 extern template istream& operator>>(istream&, _Setbase);
529 extern template istream& operator>>(istream&, _Setprecision);
530 extern template istream& operator>>(istream&, _Setw);
531
532#ifdef _GLIBCXX_USE_WCHAR_T
533 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
534 extern template wostream& operator<<(wostream&, _Setiosflags);
535 extern template wostream& operator<<(wostream&, _Resetiosflags);
536 extern template wostream& operator<<(wostream&, _Setbase);
537 extern template wostream& operator<<(wostream&, _Setprecision);
538 extern template wostream& operator<<(wostream&, _Setw);
539 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
540 extern template wistream& operator>>(wistream&, _Setiosflags);
541 extern template wistream& operator>>(wistream&, _Resetiosflags);
542 extern template wistream& operator>>(wistream&, _Setbase);
543 extern template wistream& operator>>(wistream&, _Setprecision);
544 extern template wistream& operator>>(wistream&, _Setw);
545#endif
546#endif
547
548_GLIBCXX_END_NAMESPACE_VERSION
549} // namespace
550
551#pragma GCC diagnostic pop
552#endif /* _GLIBCXX_IOMANIP */
iosfwd
locale
version.h
ios_base.h
quoted_string.h
requires_hosted.h
c++config.h
std::ostream
basic_ostream< char > ostream
Base class for char output streams.
Definition iosfwd:145
std::istream
basic_istream< char > istream
Base class for char input streams.
Definition iosfwd:142
std::wostream
basic_ostream< wchar_t > wostream
Base class for wchar_t output streams.
Definition iosfwd:185
std::wistream
basic_istream< wchar_t > wistream
Base class for wchar_t input streams.
Definition iosfwd:182
std::use_facet
const _Facet & use_facet(const locale &__loc)
Return a facet.
Definition locale_classes.tcc:220
std
ISO C++ entities toplevel namespace is std.
std::put_time
_Put_time< _CharT > put_time(const std::tm *__tmb, const _CharT *__fmt)
Extended manipulator for formatting time.
Definition iomanip:370
std::resetiosflags
_Resetiosflags resetiosflags(ios_base::fmtflags __mask)
Manipulator for setf.
Definition iomanip:76
std::get_money
_Get_money< _MoneyT > get_money(_MoneyT &__mon, bool __intl=false)
Extended manipulator for extracting money.
Definition iomanip:271
std::setw
_Setw setw(int __n)
Manipulator for width.
Definition iomanip:237
std::setprecision
_Setprecision setprecision(int __n)
Manipulator for precision.
Definition iomanip:207
std::setfill
_Setfill< _CharT > setfill(_CharT __c)
Manipulator for fill.
Definition iomanip:175
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:1658
std::put_money
_Put_money< _MoneyT > put_money(const _MoneyT &__mon, bool __intl=false)
Extended manipulator for inserting money.
Definition iomanip:318
std::setiosflags
_Setiosflags setiosflags(ios_base::fmtflags __mask)
Manipulator for setf.
Definition iomanip:106
std::operator<<
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1754
std::get_time
_Get_time< _CharT > get_time(std::tm *__tmb, const _CharT *__fmt)
Extended manipulator for extracting time.
Definition iomanip:425
std::setbase
_Setbase setbase(int __base)
Manipulator for setf.
Definition iomanip:137
std::basic_ios::fill
char_type fill() const
Retrieves the empty character.
Definition basic_ios.h:388
std::basic_ostream
Template class basic_ostream.
Definition ostream.h:67
std::basic_string_view
A non-owning reference to a string.
Definition string_view:113
std::basic_string
Managing sequences of characters and character-like objects.
Definition basic_string.h:94
std::ios_base::setf
fmtflags setf(fmtflags __fmtfl)
Setting new format flags.
Definition ios_base.h:721
std::ios_base::fmtflags
_Ios_Fmtflags fmtflags
This is a bitmask type.
Definition ios_base.h:378
std::ios_base::iostate
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:453
std::ios_base::hex
static const fmtflags hex
Converts integer input or generates integer output in hexadecimal base.
Definition ios_base.h:390
std::ios_base::basefield
static const fmtflags basefield
A mask of dec|oct|hex. Useful for the 2-arg form of setf.
Definition ios_base.h:436
std::ios_base::precision
streamsize precision() const
Flags access.
Definition ios_base.h:765
std::ios_base::dec
static const fmtflags dec
Converts integer input or generates integer output in decimal base.
Definition ios_base.h:384
std::ios_base::goodbit
static const iostate goodbit
Indicates all is well.
Definition ios_base.h:468
std::ios_base::badbit
static const iostate badbit
Indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition ios_base.h:457
std::ios_base::width
streamsize width() const
Flags access.
Definition ios_base.h:789
std::ios_base::oct
static const fmtflags oct
Converts integer input or generates integer output in octal base.
Definition ios_base.h:402
std::__detail::_Quoted_string
Struct for delimited strings.
Definition quoted_string.h:52