diff --git a/stl/inc/format b/stl/inc/format index b0b97ca0575..d5e6de99b86 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2967,6 +2967,7 @@ _NODISCARD _OutputIt _Fmt_write( auto _Format = chars_format::general; auto _Exponent = 'e'; auto _Precision = _Specs._Precision; + auto _None_type = false; switch (_Specs._Type) { case 'A': @@ -3003,6 +3004,9 @@ _NODISCARD _OutputIt _Fmt_write( } _Format = chars_format::general; break; + default: + _None_type = true; + break; } // Consider the powers of 2 in decimal: @@ -3043,7 +3047,11 @@ _NODISCARD _OutputIt _Fmt_write( _Result.ptr += 3; } else { if (_Precision == -1) { - _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format); + if (_None_type) { + _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value); + } else { + _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format); + } } else { _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format, _Precision); } diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index 7dea640f249..f516e4735e7 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -1506,6 +1506,12 @@ constexpr bool test_format_string() { return true; } +// Also test GH-4319: incorrect output for some floating-point values +template +void test_gh_4319() { + assert(format(STR("{:}"), 12345678.0) == STR("12345678")); +} + void test() { test_simple_formatting(); test_simple_formatting(); @@ -1582,6 +1588,9 @@ void test() { test_localized_char(); test_localized_char(); test_localized_char(); + + test_gh_4319(); + test_gh_4319(); } int main() {