libstdc++
charconv
Go to the documentation of this file.
1// Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
2
3// Copyright (C) 2017-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/charconv
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wpedantic" // __int128
38#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
39
40#include <bits/requires_hosted.h> // for error codes
41
42// As an extension we support <charconv> in C++14, but this header should not
43// be included by any other library headers in C++14 mode. This ensures that
44// the names defined in this header are not added to namespace std unless a
45// user explicitly includes <charconv> in C++14 code.
46#if __cplusplus >= 201402L
47
48#include <type_traits>
49#include <bit> // for __bit_width
50#include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
51#include <bits/error_constants.h> // for std::errc
52#include <ext/numeric_traits.h>
53
54#define __glibcxx_want_to_chars
55#define __glibcxx_want_constexpr_charconv
56#include <bits/version.h>
57
58namespace std _GLIBCXX_VISIBILITY(default)
59{
60_GLIBCXX_BEGIN_NAMESPACE_VERSION
61
62 /// Result type of std::to_chars
64 {
65 char* ptr;
66 errc ec;
67
68#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
69 friend bool
70 operator==(const to_chars_result&, const to_chars_result&) = default;
71#endif
72#if __cplusplus > 202302L
73 constexpr explicit operator bool() const noexcept { return ec == errc{}; }
74#endif
75 };
76
77 /// Result type of std::from_chars
79 {
80 const char* ptr;
81 errc ec;
82
83#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
84 friend bool
85 operator==(const from_chars_result&, const from_chars_result&) = default;
86#endif
87#if __cplusplus > 202302L
88 constexpr explicit operator bool() const noexcept { return ec == errc{}; }
89#endif
90 };
91
92namespace __detail
93{
94 // Pick an unsigned type of suitable size. This is used to reduce the
95 // number of specializations of __to_chars_len, __to_chars etc. that
96 // get instantiated. For example, to_chars<char> and to_chars<short>
97 // and to_chars<unsigned> will all use the same code, and so will
98 // to_chars<long> when sizeof(int) == sizeof(long).
99 template<typename _Tp>
100 constexpr auto
101 __to_chars_unsigned_type()
102 {
103 if constexpr (sizeof(_Tp) <= sizeof(unsigned int))
104 return __type_identity<unsigned int>{};
105 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
106 return __type_identity<unsigned long>{};
107 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
108 return __type_identity<unsigned long long>{};
109#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
110 else if constexpr (sizeof(_Tp) <= sizeof(unsigned __int128))
111 return __type_identity<unsigned __int128>{};
112#endif
113 }
114
115 template<typename _Tp>
116 using __unsigned_least_t
117 = typename decltype(__to_chars_unsigned_type<_Tp>())::type;
118
119 // Generic implementation for arbitrary bases.
120 // Defined in <bits/charconv.h>.
121 template<typename _Tp>
122 constexpr unsigned
123 __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
124
125 template<typename _Tp>
126 constexpr unsigned
127 __to_chars_len_2(_Tp __value) noexcept
128 { return std::__bit_width(__value); }
129
130 // Generic implementation for arbitrary bases.
131 template<typename _Tp>
132 constexpr to_chars_result
133 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
134 {
135 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
136
137 to_chars_result __res;
138
139 const unsigned __len = __to_chars_len(__val, __base);
140
141 if (__builtin_expect(size_t(__last - __first) < __len, 0))
142 {
143 __res.ptr = __last;
144 __res.ec = errc::value_too_large;
145 return __res;
146 }
147
148 unsigned __pos = __len - 1;
149
150 constexpr char __digits[] = {
151 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
152 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
153 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
154 'u', 'v', 'w', 'x', 'y', 'z'
155 };
156
157 while (__val >= (unsigned)__base)
158 {
159 auto const __quo = __val / __base;
160 auto const __rem = __val % __base;
161 __first[__pos--] = __digits[__rem];
162 __val = __quo;
163 }
164 *__first = __digits[__val];
165
166 __res.ptr = __first + __len;
167 __res.ec = {};
168 return __res;
169 }
170
171 template<typename _Tp>
172 constexpr to_chars_result
173 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
174 {
175 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
176
177 to_chars_result __res;
178
179 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
180
181 if (__builtin_expect(size_t(__last - __first) < __len, 0))
182 {
183 __res.ptr = __last;
184 __res.ec = errc::value_too_large;
185 return __res;
186 }
187
188 constexpr char __digits[] = {
189 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
190 'a', 'b', 'c', 'd', 'e', 'f'
191 };
192 unsigned __pos = __len - 1;
193 while (__val >= 0x100)
194 {
195 auto __num = __val & 0xF;
196 __val >>= 4;
197 __first[__pos] = __digits[__num];
198 __num = __val & 0xF;
199 __val >>= 4;
200 __first[__pos - 1] = __digits[__num];
201 __pos -= 2;
202 }
203 if (__val >= 0x10)
204 {
205 const auto __num = __val & 0xF;
206 __val >>= 4;
207 __first[1] = __digits[__num];
208 __first[0] = __digits[__val];
209 }
210 else
211 __first[0] = __digits[__val];
212 __res.ptr = __first + __len;
213 __res.ec = {};
214 return __res;
215 }
216
217 template<typename _Tp>
218 constexpr to_chars_result
219 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
220 {
221 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
222
223 to_chars_result __res;
224
225 const unsigned __len = __to_chars_len(__val, 10);
226
227 if (__builtin_expect(size_t(__last - __first) < __len, 0))
228 {
229 __res.ptr = __last;
230 __res.ec = errc::value_too_large;
231 return __res;
232 }
233
234 __detail::__to_chars_10_impl(__first, __len, __val);
235 __res.ptr = __first + __len;
236 __res.ec = {};
237 return __res;
238 }
239
240 template<typename _Tp>
241 constexpr to_chars_result
242 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
243 {
244 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
245
246 to_chars_result __res;
247 unsigned __len = 0;
248
249 if constexpr (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
250 {
251#if __cpp_0o_octals >= 202611L
252 __len = __val > 0o77777u ? 6u
253 : __val > 0o7777u ? 5u
254 : __val > 0o777u ? 4u
255 : __val > 0o77u ? 3u
256 : __val > 0o7u ? 2u
257 : 1u;
258#else
259 __len = __val > 077777u ? 6u
260 : __val > 07777u ? 5u
261 : __val > 0777u ? 4u
262 : __val > 077u ? 3u
263 : __val > 07u ? 2u
264 : 1u;
265#endif
266 }
267 else
268 __len = (__to_chars_len_2(__val) + 2) / 3;
269
270 if (__builtin_expect(size_t(__last - __first) < __len, 0))
271 {
272 __res.ptr = __last;
273 __res.ec = errc::value_too_large;
274 return __res;
275 }
276
277 unsigned __pos = __len - 1;
278 while (__val >= 8 * 8)
279 {
280 auto __num = __val & 7;
281 __val >>= 3;
282 __first[__pos] = '0' + __num;
283 __num = __val & 7;
284 __val >>= 3;
285 __first[__pos - 1] = '0' + __num;
286 __pos -= 2;
287 }
288 if (__val >= 8)
289 {
290 auto const __num = __val & 7;
291 __val >>= 3;
292 __first[1] = '0' + __num;
293 __first[0] = '0' + __val;
294 }
295 else
296 __first[0] = '0' + __val;
297 __res.ptr = __first + __len;
298 __res.ec = {};
299 return __res;
300 }
301
302 template<typename _Tp>
303 constexpr to_chars_result
304 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
305 {
306 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
307
308 to_chars_result __res;
309
310 const unsigned __len = __to_chars_len_2(__val);
311
312 if (__builtin_expect(size_t(__last - __first) < __len, 0))
313 {
314 __res.ptr = __last;
315 __res.ec = errc::value_too_large;
316 return __res;
317 }
318
319 unsigned __pos = __len - 1;
320
321 while (__pos)
322 {
323 __first[__pos--] = '0' + (__val & 1);
324 __val >>= 1;
325 }
326 // First digit is always '1' because __to_chars_len_2 skips
327 // leading zero bits and std::to_chars handles zero values
328 // directly.
329 __first[0] = '1';
330
331 __res.ptr = __first + __len;
332 __res.ec = {};
333 return __res;
334 }
335
336} // namespace __detail
337
338 template<typename _Tp>
339 constexpr to_chars_result
340 __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
341 {
342 __glibcxx_assert(2 <= __base && __base <= 36);
343
344 using _Up = __detail::__unsigned_least_t<_Tp>;
345 _Up __unsigned_val = __value;
346
347 if (__first >= __last) [[__unlikely__]]
348 return { __last, errc::value_too_large };
349
350 if (__value == 0)
351 {
352 *__first = '0';
353 return { __first + 1, errc{} };
354 }
355 else if constexpr (std::is_signed<_Tp>::value)
356 if (__value < 0)
357 {
358 *__first++ = '-';
359 __unsigned_val = _Up(~__value) + _Up(1);
360 }
361
362 switch (__base)
363 {
364 case 16:
365 return __detail::__to_chars_16(__first, __last, __unsigned_val);
366 case 10:
367 return __detail::__to_chars_10(__first, __last, __unsigned_val);
368 case 8:
369 return __detail::__to_chars_8(__first, __last, __unsigned_val);
370 case 2:
371 return __detail::__to_chars_2(__first, __last, __unsigned_val);
372 default:
373 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
374 }
375 }
376
377#define _GLIBCXX_TO_CHARS(T) \
378 _GLIBCXX23_CONSTEXPR inline to_chars_result \
379 to_chars(char* __first, char* __last, T __value, int __base = 10) \
380 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
381_GLIBCXX_TO_CHARS(char)
382_GLIBCXX_TO_CHARS(signed char)
383_GLIBCXX_TO_CHARS(unsigned char)
384_GLIBCXX_TO_CHARS(signed short)
385_GLIBCXX_TO_CHARS(unsigned short)
386_GLIBCXX_TO_CHARS(signed int)
387_GLIBCXX_TO_CHARS(unsigned int)
388_GLIBCXX_TO_CHARS(signed long)
389_GLIBCXX_TO_CHARS(unsigned long)
390_GLIBCXX_TO_CHARS(signed long long)
391_GLIBCXX_TO_CHARS(unsigned long long)
392#if defined(__GLIBCXX_TYPE_INT_N_0)
393_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
394_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
395#endif
396#if defined(__GLIBCXX_TYPE_INT_N_1)
397_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
398_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
399#endif
400#if defined(__GLIBCXX_TYPE_INT_N_2)
401_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
402_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
403#endif
404#if defined(__GLIBCXX_TYPE_INT_N_3)
405_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
406_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
407#endif
408#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
409_GLIBCXX_TO_CHARS(signed __int128)
410_GLIBCXX_TO_CHARS(unsigned __int128)
411#endif
412#undef _GLIBCXX_TO_CHARS
413
414 // _GLIBCXX_RESOLVE_LIB_DEFECTS
415 // 3266. to_chars(bool) should be deleted
416 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
417
418namespace __detail
419{
420 template<typename _Tp>
421 constexpr bool
422 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
423 {
424 if (__builtin_mul_overflow(__val, __base, &__val)
425 || __builtin_add_overflow(__val, __c, &__val))
426 return false;
427 return true;
428 }
429
430 template<bool _DecOnly>
431 struct __from_chars_alnum_to_val_table
432 {
433 struct type { unsigned char __data[1u << __CHAR_BIT__] = {}; };
434
435 // Construct and return a lookup table that maps 0-9, A-Z and a-z to their
436 // corresponding base-36 value and maps all other characters to 127.
437 static constexpr type
438 _S_make_table()
439 {
440 constexpr unsigned char __lower_letters[27] = "abcdefghijklmnopqrstuvwxyz";
441 constexpr unsigned char __upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
442 type __table;
443 for (auto& __entry : __table.__data)
444 __entry = 127;
445 for (int __i = 0; __i < 10; ++__i)
446 __table.__data['0' + __i] = __i;
447 for (int __i = 0; __i < 26; ++__i)
448 {
449 __table.__data[__lower_letters[__i]] = 10 + __i;
450 __table.__data[__upper_letters[__i]] = 10 + __i;
451 }
452 return __table;
453 }
454
455 // This initializer is made superficially dependent in order
456 // to prevent the compiler from wastefully constructing the
457 // table ahead of time when it's not needed.
458 static constexpr type value = (_DecOnly, _S_make_table());
459 };
460
461#if ! __cpp_inline_variables
462 template<bool _DecOnly>
463 const typename __from_chars_alnum_to_val_table<_DecOnly>::type
464 __from_chars_alnum_to_val_table<_DecOnly>::value;
465#endif
466
467 // If _DecOnly is true: if the character is a decimal digit, then
468 // return its corresponding base-10 value, otherwise return a value >= 127.
469 // If _DecOnly is false: if the character is an alphanumeric digit, then
470 // return its corresponding base-36 value, otherwise return a value >= 127.
471 template<bool _DecOnly = false>
472 _GLIBCXX20_CONSTEXPR unsigned char
473 __from_chars_alnum_to_val(unsigned char __c)
474 {
475 if constexpr (_DecOnly)
476 return static_cast<unsigned char>(__c - '0');
477 else
478 return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
479 }
480
481 /// std::from_chars implementation for integers in a power-of-two base.
482 /// If _DecOnly is true, then we may assume __base is at most 8.
483 template<bool _DecOnly, typename _Tp>
484 _GLIBCXX23_CONSTEXPR bool
485 __from_chars_pow2_base(const char*& __first, const char* __last, _Tp& __val,
486 int __base)
487 {
488 static_assert(is_integral<_Tp>::value, "implementation bug");
489 static_assert(is_unsigned<_Tp>::value, "implementation bug");
490
491 // __glibcxx_assert((__base & (__base - 1)) == 0);
492 // __glibcxx_assert(_DecOnly ? __base <= 8 : __base <= 32);
493 const int __log2_base = __countr_zero(unsigned(__base & 0x3f));
494
495 const ptrdiff_t __len = __last - __first;
496 ptrdiff_t __i = 0;
497 while (__i < __len && __first[__i] == '0')
498 ++__i;
499 const ptrdiff_t __leading_zeroes = __i;
500 if (__i >= __len) [[__unlikely__]]
501 {
502 __first += __i;
503 return true;
504 }
505
506 // Remember the leading significant digit value if necessary.
507 unsigned char __leading_c = 0;
508 if (__base != 2)
509 {
510 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
511 // __glibcxx_assert(__leading_c != 0);
512 if (__leading_c >= __base) [[__unlikely__]]
513 {
514 __first += __i;
515 return true;
516 }
517 __val = __leading_c;
518 ++__i;
519 }
520
521 for (; __i < __len; ++__i)
522 {
523 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
524 if (__c >= __base)
525 break;
526 __val = (__val << __log2_base) | __c;
527 }
528 __first += __i;
529 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
530 if (__base != 2)
531 // Compensate for a leading significant digit that didn't use all
532 // of its available bits.
533 __significant_bits -= __log2_base - __bit_width(__leading_c);
534 // __glibcxx_assert(__significant_bits >= 0);
535 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
536 }
537
538 /// std::from_chars implementation for integers in any base.
539 /// If _DecOnly is true, then we may assume __base is at most 10.
540 template<bool _DecOnly, typename _Tp>
541 constexpr bool
542 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
543 int __base)
544 {
545 // __glibcxx_assert(_DecOnly ? __base <= 10 : __base <= 36);
546
547 const int __bits_per_digit = __bit_width(unsigned(__base & 0x3f));
548 int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
549 for (; __first != __last; ++__first)
550 {
551 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
552 if (__c >= __base)
553 return true;
554
555 __unused_bits_lower_bound -= __bits_per_digit;
556 if (__unused_bits_lower_bound >= 0) [[__likely__]]
557 // We're definitely not going to overflow.
558 __val = __val * __base + __c;
559 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
560 {
561 while (++__first != __last
562 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
563 ;
564 return false;
565 }
566 }
567 return true;
568 }
569
570} // namespace __detail
571
572 /// std::from_chars for integer types.
573 template<typename _Tp,
575 is_same<char, _Tp>>::value, int> = 0>
576 _GLIBCXX23_CONSTEXPR from_chars_result
577 from_chars(const char* __first, const char* __last, _Tp& __value,
578 int __base = 10)
579 {
580 __glibcxx_assert(2 <= __base && __base <= 36);
581
582 from_chars_result __res{__first, {}};
583
584 int __sign = 1;
585 if constexpr (std::is_signed<_Tp>::value)
586 if (__first != __last && *__first == '-')
587 {
588 __sign = -1;
589 ++__first;
590 }
591
592 using _Up = __detail::__unsigned_least_t<_Tp>;
593 _Up __val = 0;
594
595 const auto __start = __first;
596 bool __valid;
597 if ((__base & (__base - 1)) == 0)
598 {
599 if (__base <= 8)
600 __valid = __detail::__from_chars_pow2_base<true>(__first, __last, __val, __base);
601 else
602 __valid = __detail::__from_chars_pow2_base<false>(__first, __last, __val, __base);
603 }
604 else if (__base <= 10)
605 __valid = __detail::__from_chars_alnum<true>(__first, __last, __val, __base);
606 else
607 __valid = __detail::__from_chars_alnum<false>(__first, __last, __val, __base);
608
609 if (__builtin_expect(__first == __start, 0))
610 __res.ec = errc::invalid_argument;
611 else
612 {
613 __res.ptr = __first;
614 if (!__valid)
615 __res.ec = errc::result_out_of_range;
616 else
617 {
618 if constexpr (std::is_signed<_Tp>::value)
619 {
620 _Tp __tmp;
621 if (__builtin_mul_overflow(__val, __sign, &__tmp))
622 __res.ec = errc::result_out_of_range;
623 else
624 __value = __tmp;
625 }
626 else
627 {
628 if constexpr (__gnu_cxx::__int_traits<_Up>::__max
629 > __gnu_cxx::__int_traits<_Tp>::__max)
630 {
631 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
632 __res.ec = errc::result_out_of_range;
633 else
634 __value = __val;
635 }
636 else
637 __value = __val;
638 }
639 }
640 }
641 return __res;
642 }
643
644 /// floating-point format for primitive numerical conversion
645 enum class chars_format
646 {
647 scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
648 };
649
650 [[nodiscard]]
651 constexpr chars_format
652 operator|(chars_format __lhs, chars_format __rhs) noexcept
653 { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
654
655 [[nodiscard]]
656 constexpr chars_format
657 operator&(chars_format __lhs, chars_format __rhs) noexcept
658 { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
659
660 [[nodiscard]]
661 constexpr chars_format
662 operator^(chars_format __lhs, chars_format __rhs) noexcept
663 { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
664
665 [[nodiscard]]
666 constexpr chars_format
667 operator~(chars_format __fmt) noexcept
668 { return (chars_format)~(unsigned)__fmt; }
669
670 constexpr chars_format&
671 operator|=(chars_format& __lhs, chars_format __rhs) noexcept
672 { return __lhs = __lhs | __rhs; }
673
674 constexpr chars_format&
675 operator&=(chars_format& __lhs, chars_format __rhs) noexcept
676 { return __lhs = __lhs & __rhs; }
677
678 constexpr chars_format&
679 operator^=(chars_format& __lhs, chars_format __rhs) noexcept
680 { return __lhs = __lhs ^ __rhs; }
681
682#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
684 from_chars(const char* __first, const char* __last, float& __value,
685 chars_format __fmt = chars_format::general) noexcept;
686
688 from_chars(const char* __first, const char* __last, double& __value,
689 chars_format __fmt = chars_format::general) noexcept;
690
692 from_chars(const char* __first, const char* __last, long double& __value,
693 chars_format __fmt = chars_format::general) noexcept;
694
695 // Library routines for 16-bit extended floating point formats
696 // using float as interchange format.
698 __from_chars_float16_t(const char* __first, const char* __last,
699 float& __value,
700 chars_format __fmt = chars_format::general) noexcept;
702 __from_chars_bfloat16_t(const char* __first, const char* __last,
703 float& __value,
704 chars_format __fmt = chars_format::general) noexcept;
705
706#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
707 && defined(__cpp_lib_to_chars)
708 inline from_chars_result
709 from_chars(const char* __first, const char* __last, _Float16& __value,
710 chars_format __fmt = chars_format::general) noexcept
711 {
712 float __val;
714 = __from_chars_float16_t(__first, __last, __val, __fmt);
715 if (__res.ec == errc{})
716 __value = _Float16(__val);
717 return __res;
718 }
719#endif
720
721#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
722 inline from_chars_result
723 from_chars(const char* __first, const char* __last, _Float32& __value,
724 chars_format __fmt = chars_format::general) noexcept
725 {
726 float __val;
727 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
728 if (__res.ec == errc{})
729 __value = _Float32(__val);
730 return __res;
731 }
732#endif
733
734#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
735 inline from_chars_result
736 from_chars(const char* __first, const char* __last, _Float64& __value,
737 chars_format __fmt = chars_format::general) noexcept
738 {
739 double __val;
740 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
741 if (__res.ec == errc{})
742 __value = _Float64(__val);
743 return __res;
744 }
745#endif
746
747#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
748 inline from_chars_result
749 from_chars(const char* __first, const char* __last, _Float128& __value,
750 chars_format __fmt = chars_format::general) noexcept
751 {
752 long double __val;
753 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
754 if (__res.ec == errc{})
755 __value = _Float128(__val);
756 return __res;
757 }
758#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
759#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
760 __extension__ from_chars_result
761 from_chars(const char* __first, const char* __last, __ieee128& __value,
762 chars_format __fmt = chars_format::general) noexcept;
763
764 inline from_chars_result
765 from_chars(const char* __first, const char* __last, _Float128& __value,
766 chars_format __fmt = chars_format::general) noexcept
767 {
768 __extension__ __ieee128 __val;
769 from_chars_result __res = from_chars(__first, __last, __val, __fmt);
770 if (__res.ec == errc{})
771 __value = _Float128(__val);
772 return __res;
773 }
774#else
776 from_chars(const char* __first, const char* __last, _Float128& __value,
777 chars_format __fmt = chars_format::general) noexcept;
778#endif
779#endif
780
781#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
782 && defined(__cpp_lib_to_chars)
783 inline from_chars_result
784 from_chars(const char* __first, const char* __last,
785 __gnu_cxx::__bfloat16_t & __value,
786 chars_format __fmt = chars_format::general) noexcept
787 {
788 float __val;
790 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
791 if (__res.ec == errc{})
792 __value = __gnu_cxx::__bfloat16_t(__val);
793 return __res;
794 }
795#endif
796#endif
797
798#if defined __cpp_lib_to_chars
799 // Floating-point std::to_chars
800
801 // Overloads for float.
802 to_chars_result to_chars(char* __first, char* __last, float __value) noexcept;
803 to_chars_result to_chars(char* __first, char* __last, float __value,
804 chars_format __fmt) noexcept;
805 to_chars_result to_chars(char* __first, char* __last, float __value,
806 chars_format __fmt, int __precision) noexcept;
807
808 // Overloads for double.
809 to_chars_result to_chars(char* __first, char* __last, double __value) noexcept;
810 to_chars_result to_chars(char* __first, char* __last, double __value,
811 chars_format __fmt) noexcept;
812 to_chars_result to_chars(char* __first, char* __last, double __value,
813 chars_format __fmt, int __precision) noexcept;
814
815 // Overloads for long double.
816 to_chars_result to_chars(char* __first, char* __last, long double __value)
817 noexcept;
818 to_chars_result to_chars(char* __first, char* __last, long double __value,
819 chars_format __fmt) noexcept;
820 to_chars_result to_chars(char* __first, char* __last, long double __value,
821 chars_format __fmt, int __precision) noexcept;
822
823 // Library routines for 16-bit extended floating point formats
824 // using float as interchange format.
825 to_chars_result __to_chars_float16_t(char* __first, char* __last,
826 float __value,
827 chars_format __fmt) noexcept;
828 to_chars_result __to_chars_bfloat16_t(char* __first, char* __last,
829 float __value,
830 chars_format __fmt) noexcept;
831
832#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
833 inline to_chars_result
834 to_chars(char* __first, char* __last, _Float16 __value) noexcept
835 {
836 return __to_chars_float16_t(__first, __last, float(__value),
837 chars_format{});
838 }
839 inline to_chars_result
840 to_chars(char* __first, char* __last, _Float16 __value,
841 chars_format __fmt) noexcept
842 { return __to_chars_float16_t(__first, __last, float(__value), __fmt); }
843 inline to_chars_result
844 to_chars(char* __first, char* __last, _Float16 __value,
845 chars_format __fmt, int __precision) noexcept
846 { return to_chars(__first, __last, float(__value), __fmt, __precision); }
847#endif
848
849#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
850 inline to_chars_result
851 to_chars(char* __first, char* __last, _Float32 __value) noexcept
852 { return to_chars(__first, __last, float(__value)); }
853 inline to_chars_result
854 to_chars(char* __first, char* __last, _Float32 __value,
855 chars_format __fmt) noexcept
856 { return to_chars(__first, __last, float(__value), __fmt); }
857 inline to_chars_result
858 to_chars(char* __first, char* __last, _Float32 __value,
859 chars_format __fmt, int __precision) noexcept
860 { return to_chars(__first, __last, float(__value), __fmt, __precision); }
861#endif
862
863#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
864 inline to_chars_result
865 to_chars(char* __first, char* __last, _Float64 __value) noexcept
866 { return to_chars(__first, __last, double(__value)); }
867 inline to_chars_result
868 to_chars(char* __first, char* __last, _Float64 __value,
869 chars_format __fmt) noexcept
870 { return to_chars(__first, __last, double(__value), __fmt); }
871 inline to_chars_result
872 to_chars(char* __first, char* __last, _Float64 __value,
873 chars_format __fmt, int __precision) noexcept
874 { return to_chars(__first, __last, double(__value), __fmt, __precision); }
875#endif
876
877#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
878 inline to_chars_result
879 to_chars(char* __first, char* __last, _Float128 __value) noexcept
880 { return to_chars(__first, __last, static_cast<long double>(__value)); }
881 inline to_chars_result
882 to_chars(char* __first, char* __last, _Float128 __value,
883 chars_format __fmt) noexcept
884 {
885 return to_chars(__first, __last, static_cast<long double>(__value), __fmt);
886 }
887 inline to_chars_result
888 to_chars(char* __first, char* __last, _Float128 __value,
889 chars_format __fmt, int __precision) noexcept
890 {
891 return to_chars(__first, __last, static_cast<long double>(__value), __fmt,
892 __precision);
893 }
894#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
895#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
896 __extension__ to_chars_result
897 to_chars(char* __first, char* __last, __float128 __value) noexcept;
898 __extension__ to_chars_result
899 to_chars(char* __first, char* __last, __float128 __value,
900 chars_format __fmt) noexcept;
901 __extension__ to_chars_result
902 to_chars(char* __first, char* __last, __float128 __value,
903 chars_format __fmt, int __precision) noexcept;
904
905 inline to_chars_result
906 to_chars(char* __first, char* __last, _Float128 __value) noexcept
907 {
908 return __extension__ to_chars(__first, __last,
909 static_cast<__float128>(__value));
910 }
911 inline to_chars_result
912 to_chars(char* __first, char* __last, _Float128 __value,
913 chars_format __fmt) noexcept
914 {
915
916 return __extension__ to_chars(__first, __last,
917 static_cast<__float128>(__value), __fmt);
918 }
919 inline to_chars_result
920 to_chars(char* __first, char* __last, _Float128 __value,
921 chars_format __fmt, int __precision) noexcept
922 {
923
924 return __extension__ to_chars(__first, __last,
925 static_cast<__float128>(__value), __fmt,
926 __precision);
927 }
928#else
929 to_chars_result to_chars(char* __first, char* __last, _Float128 __value)
930 noexcept;
931 to_chars_result to_chars(char* __first, char* __last, _Float128 __value,
932 chars_format __fmt) noexcept;
933 to_chars_result to_chars(char* __first, char* __last, _Float128 __value,
934 chars_format __fmt, int __precision) noexcept;
935#endif
936#endif
937
938#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
939 inline to_chars_result
940 to_chars(char* __first, char* __last,
941 __gnu_cxx::__bfloat16_t __value) noexcept
942 {
943 return __to_chars_bfloat16_t(__first, __last, float(__value),
944 chars_format{});
945 }
946 inline to_chars_result
947 to_chars(char* __first, char* __last, __gnu_cxx::__bfloat16_t __value,
948 chars_format __fmt) noexcept
949 { return __to_chars_bfloat16_t(__first, __last, float(__value), __fmt); }
950 inline to_chars_result
951 to_chars(char* __first, char* __last, __gnu_cxx::__bfloat16_t __value,
952 chars_format __fmt, int __precision) noexcept
953 { return to_chars(__first, __last, float(__value), __fmt, __precision); }
954#endif
955#endif
956
957_GLIBCXX_END_NAMESPACE_VERSION
958} // namespace std
959#endif // C++14
960#pragma GCC diagnostic pop
961#endif // _GLIBCXX_CHARCONV
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2971
ISO C++ entities toplevel namespace is std.
ios_base & scientific(ios_base &__base)
Calls base.setf(ios_base::scientific, ios_base::floatfield).
Definition ios_base.h:1127
ios_base & hex(ios_base &__base)
Calls base.setf(ios_base::hex, ios_base::basefield).
Definition ios_base.h:1102
chars_format
floating-point format for primitive numerical conversion
Definition charconv:646
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1682
ios_base & fixed(ios_base &__base)
Calls base.setf(ios_base::fixed, ios_base::floatfield).
Definition ios_base.h:1119
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1672
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1662
constexpr from_chars_result from_chars(const char *__first, const char *__last, _Tp &__value, int __base=10)
std::from_chars for integer types.
Definition charconv:577
Implementation details not part of the namespace std interface.
constexpr bool __from_chars_alnum(const char *&__first, const char *__last, _Tp &__val, int __base)
std::from_chars implementation for integers in any base. If _DecOnly is true, then we may assume __ba...
Definition charconv:542
constexpr bool __from_chars_pow2_base(const char *&__first, const char *__last, _Tp &__val, int __base)
std::from_chars implementation for integers in a power-of-two base. If _DecOnly is true,...
Definition charconv:485
constexpr _Iterator __base(_Iterator __it)
Result type of std::to_chars.
Definition charconv:64
Result type of std::from_chars.
Definition charconv:79
is_integral
Definition type_traits:566
is_signed
Definition type_traits:1085
is_unsigned
Definition type_traits:1091