diff --git a/stl/inc/format b/stl/inc/format index 3e89553d827..24e45376e2b 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -1421,15 +1421,10 @@ public: template _NODISCARD constexpr unsigned long long operator()(const _Ty _Value) const { if constexpr (is_integral_v<_Ty>) { - bool _Positive; - if constexpr (same_as<_Ty, bool>) { // avoid "bool > 0", which triggers C4804 - _Positive = _Value != 0; - } else { - _Positive = _Value > 0; - } - - if (!_Positive) { - _Throw_format_error("width is not positive."); + if constexpr (is_signed_v<_Ty>) { + if (_Value < 0) { + _Throw_format_error("Negative width."); + } } return static_cast(_Value); } else { diff --git a/tests/std/tests/P0645R10_text_formatting_death/test.cpp b/tests/std/tests/P0645R10_text_formatting_death/test.cpp index e72fab0920f..e09eba9dc28 100644 --- a/tests/std/tests/P0645R10_text_formatting_death/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_death/test.cpp @@ -17,8 +17,8 @@ void test_case_advance_no_range() { context.advance_to(other_format_string.begin()); } -void test_case_zero_dynamic_width() { - (void) format("{:{}}", 42, 0); +void test_case_negative_dynamic_width() { + (void) format("{:{}}", 42, -2); } int main(int argc, char* argv[]) { @@ -27,7 +27,7 @@ int main(int argc, char* argv[]) { #if _ITERATOR_DEBUG_LEVEL != 0 exec.add_death_tests({ test_case_advance_no_range, - test_case_zero_dynamic_width, + test_case_negative_dynamic_width, }); #endif // _ITERATOR_DEBUG_LEVEL != 0 diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index c1835218ad5..f819949870f 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -1287,6 +1287,8 @@ void libfmt_formatter_test_runtime_width() { == STR(" 0")); // behavior differs from libfmt, but conforms throw_helper(STR("{0:{1}}"), 0, 0.0); + assert(format(STR("{0:{1}}"), 42, 0) == STR("42")); // LWG-3721: zero dynamic width is OK + assert(format(STR("{0:{1}}"), -42, 4) == STR(" -42")); assert(format(STR("{0:{1}}"), 42u, 5) == STR(" 42")); assert(format(STR("{0:{1}}"), -42l, 6) == STR(" -42"));