Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -1421,15 +1421,10 @@ public:
template <class _Ty>
_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<unsigned long long>(_Value);
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/std/tests/P0645R10_text_formatting_death/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Comment thread
StephanTLavavej marked this conversation as resolved.
(void) format("{:{}}", 42, -2);
}

int main(int argc, char* argv[]) {
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down