1// Standard stream manipulators -*- C++ -*-
3// Copyright (C) 1997-2025 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25/** @file include/iomanip
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 27.6.3 Standard manipulators
33#ifndef _GLIBCXX_IOMANIP
34#define _GLIBCXX_IOMANIP 1
37#pragma GCC system_header
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Wc++11-extensions"
43#include <bits/requires_hosted.h> // iostreams
45#include <bits/c++config.h>
47#include <bits/ios_base.h>
49#define __glibcxx_want_quoted_string_io
50#include <bits/version.h>
52#if __cplusplus >= 201103L
54#if __cplusplus > 201103L
55#include <bits/quoted_string.h>
59namespace std _GLIBCXX_VISIBILITY(default)
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
63 // [27.6.3] standard manipulators
66 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
69 * @brief Manipulator for @c setf.
70 * @param __mask A format flags mask.
72 * Sent to a stream object, this manipulator resets the specified flags,
73 * via @e stream.setf(0,__mask).
76 resetiosflags(ios_base::fmtflags __mask)
77 { return { __mask }; }
79 template<typename _CharT, typename _Traits>
80 inline basic_istream<_CharT, _Traits>&
81 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
83 __is.setf(ios_base::fmtflags(0), __f._M_mask);
87 template<typename _CharT, typename _Traits>
88 inline basic_ostream<_CharT, _Traits>&
89 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
91 __os.setf(ios_base::fmtflags(0), __f._M_mask);
96 struct _Setiosflags { ios_base::fmtflags _M_mask; };
99 * @brief Manipulator for @c setf.
100 * @param __mask A format flags mask.
102 * Sent to a stream object, this manipulator sets the format flags
106 setiosflags(ios_base::fmtflags __mask)
107 { return { __mask }; }
109 template<typename _CharT, typename _Traits>
110 inline basic_istream<_CharT, _Traits>&
111 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
113 __is.setf(__f._M_mask);
117 template<typename _CharT, typename _Traits>
118 inline basic_ostream<_CharT, _Traits>&
119 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
121 __os.setf(__f._M_mask);
126 struct _Setbase { int _M_base; };
129 * @brief Manipulator for @c setf.
130 * @param __base A numeric base.
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.
138 { return { __base }; }
140 template<typename _CharT, typename _Traits>
141 inline basic_istream<_CharT, _Traits>&
142 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
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);
151 template<typename _CharT, typename _Traits>
152 inline basic_ostream<_CharT, _Traits>&
153 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
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);
163 template<typename _CharT>
164 struct _Setfill { _CharT _M_c; };
167 * @brief Manipulator for @c fill.
168 * @param __c The new fill character.
170 * Sent to a stream object, this manipulator calls @c fill(__c) for that
173 template<typename _CharT>
174 inline _Setfill<_CharT>
178 template<typename _CharT, typename _Traits>
179 __attribute__((__deprecated__("'std::setfill' should only be used with "
181 inline basic_istream<_CharT, _Traits>&
182 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
188 template<typename _CharT, typename _Traits>
189 inline basic_ostream<_CharT, _Traits>&
190 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
197 struct _Setprecision { int _M_n; };
200 * @brief Manipulator for @c precision.
201 * @param __n The new precision.
203 * Sent to a stream object, this manipulator calls @c precision(__n) for
207 setprecision(int __n)
210 template<typename _CharT, typename _Traits>
211 inline basic_istream<_CharT, _Traits>&
212 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
214 __is.precision(__f._M_n);
218 template<typename _CharT, typename _Traits>
219 inline basic_ostream<_CharT, _Traits>&
220 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
222 __os.precision(__f._M_n);
227 struct _Setw { int _M_n; };
230 * @brief Manipulator for @c width.
231 * @param __n The new width.
233 * Sent to a stream object, this manipulator calls @c width(__n) for
240 template<typename _CharT, typename _Traits>
241 inline basic_istream<_CharT, _Traits>&
242 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
244 __is.width(__f._M_n);
248 template<typename _CharT, typename _Traits>
249 inline basic_ostream<_CharT, _Traits>&
250 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
252 __os.width(__f._M_n);
256#if __cplusplus >= 201103L
258 template<typename _MoneyT>
259 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
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
267 * Sent to a stream object, this manipulator extracts @a __mon.
269 template<typename _MoneyT>
270 inline _Get_money<_MoneyT>
271 get_money(_MoneyT& __mon, bool __intl = false)
272 { return { __mon, __intl }; }
274 template<typename _CharT, typename _Traits, typename _MoneyT>
275 basic_istream<_CharT, _Traits>&
276 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
278 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
281 ios_base::iostate __err = ios_base::goodbit;
284 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
285 typedef money_get<_CharT, _Iter> _MoneyGet;
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);
291 __catch(__cxxabiv1::__forced_unwind&)
293 __is._M_setstate(ios_base::badbit);
294 __throw_exception_again;
297 { __is._M_setstate(ios_base::badbit); }
299 __is.setstate(__err);
305 template<typename _MoneyT>
306 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
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
314 * Sent to a stream object, this manipulator inserts @a __mon.
316 template<typename _MoneyT>
317 inline _Put_money<_MoneyT>
318 put_money(const _MoneyT& __mon, bool __intl = false)
319 { return { __mon, __intl }; }
321 template<typename _CharT, typename _Traits, typename _MoneyT>
322 basic_ostream<_CharT, _Traits>&
323 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
325 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
328 ios_base::iostate __err = ios_base::goodbit;
331 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
332 typedef money_put<_CharT, _Iter> _MoneyPut;
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;
339 __catch(__cxxabiv1::__forced_unwind&)
341 __os._M_setstate(ios_base::badbit);
342 __throw_exception_again;
345 { __os._M_setstate(ios_base::badbit); }
347 __os.setstate(__err);
352 template<typename _CharT>
355 const std::tm* _M_tmb;
356 const _CharT* _M_fmt;
360 * @brief Extended manipulator for formatting time.
362 * This manipulator uses time_put::put to format time.
365 * @param __tmb struct tm time data to format.
366 * @param __fmt format string.
368 template<typename _CharT>
369 inline _Put_time<_CharT>
370 put_time(const std::tm* __tmb, const _CharT* __fmt)
371 { return { __tmb, __fmt }; }
373 template<typename _CharT, typename _Traits>
374 basic_ostream<_CharT, _Traits>&
375 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
377 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
380 ios_base::iostate __err = ios_base::goodbit;
383 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
384 typedef time_put<_CharT, _Iter> _TimePut;
386 const _CharT* const __fmt_end = __f._M_fmt +
387 _Traits::length(__f._M_fmt);
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;
394 __catch(__cxxabiv1::__forced_unwind&)
396 __os._M_setstate(ios_base::badbit);
397 __throw_exception_again;
400 { __os._M_setstate(ios_base::badbit); }
402 __os.setstate(__err);
407 template<typename _CharT>
411 const _CharT* _M_fmt;
415 * @brief Extended manipulator for extracting time.
417 * This manipulator uses time_get::get to extract time.
420 * @param __tmb struct to extract the time data to.
421 * @param __fmt format string.
423 template<typename _CharT>
424 inline _Get_time<_CharT>
425 get_time(std::tm* __tmb, const _CharT* __fmt)
426 { return { __tmb, __fmt }; }
428 template<typename _CharT, typename _Traits>
429 basic_istream<_CharT, _Traits>&
430 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
432 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
435 ios_base::iostate __err = ios_base::goodbit;
438 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
439 typedef time_get<_CharT, _Iter> _TimeGet;
441 const _CharT* const __fmt_end = __f._M_fmt +
442 _Traits::length(__f._M_fmt);
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);
448 __catch(__cxxabiv1::__forced_unwind&)
450 __is._M_setstate(ios_base::badbit);
451 __throw_exception_again;
454 { __is._M_setstate(ios_base::badbit); }
456 __is.setstate(__err);
461#ifdef __cpp_lib_quoted_string_io // C++ >= 14 && HOSTED
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.
470 template<typename _CharT>
472 quoted(const _CharT* __string,
473 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
475 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
479 template<typename _CharT, typename _Traits, typename _Alloc>
481 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
482 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
484 return __detail::_Quoted_string<
485 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
486 __string, __delim, __escape);
489 template<typename _CharT, typename _Traits, typename _Alloc>
491 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
492 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
494 return __detail::_Quoted_string<
495 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
496 __string, __delim, __escape);
499#if __cplusplus >= 201703L
500 // _GLIBCXX_RESOLVE_LIB_DEFECTS
501 // 2785. quoted should work with basic_string_view
502 template<typename _CharT, typename _Traits>
504 quoted(basic_string_view<_CharT, _Traits> __sv,
505 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
507 return __detail::_Quoted_string<
508 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
511#endif // __cpp_lib_quoted_string_io
513#endif // __cplusplus >= 201103L
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);
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);
548_GLIBCXX_END_NAMESPACE_VERSION
551#pragma GCC diagnostic pop
552#endif /* _GLIBCXX_IOMANIP */