This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++23 status.

3721. Allow an arg-id with a value of zero for width in std-format-spec

Section: 22.14.2.2 [format.string.std] Status: C++23 Submitter: Mark de Wever Opened: 2022-06-19 Last modified: 2023-11-22

Priority: 3

View other active issues in [format.string.std].

View all other issues in [format.string.std].

View all issues with C++23 status.

Discussion:

Per 22.14.2.2 [format.string.std]/7

If { arg-idopt } is used in a width or precision, the value of the corresponding formatting argument is used in its place. If the corresponding formatting argument is not of integral type, or its value is negative for precision or non-positive for width, an exception of type format_error is thrown.

During a libc++ code review Victor mentioned it would be nice to allow zero as a valid value for the arg-id when used for the width. This would simplify the code by having the same requirements for the arg-id for the width and precision fields. A width of zero has no effect on the output.

In the std-format-spec the width is restricted to a positive-integer to avoid parsing ambiguity with the zero-padding option. This ambiguity doesn't happen using an arg-id with the value zero. Therefore I only propose to change the arg-id's requirement.

Note the Standard doesn't specify the width field's effect on the output. Specifically [tab:format.align] doesn't refer to the width field. This is one of the items addressed by P2572. The proposed resolution works in combination with the wording of that paper.

[2022-07-08; Reflector poll]

Set priority to 3 after reflector poll.

[2022-07-11; Reflector poll]

Set status to Tentatively Ready after seven votes in favour during reflector poll.

[2022-07-15; LWG telecon: move to Ready]

[2022-07-25 Approved at July 2022 virtual plenary. Status changed: Ready → WP.]

Proposed resolution:

This wording is relative to N4910.

  1. Modify 22.14.2.2 [format.string.std] as indicated:

    -7- If { arg-idopt } is used in a width or precision, the value of the corresponding formatting argument is used in its place. If the corresponding formatting argument is not of integral type, or its value is negative for precision or non-positive for width, an exception of type format_error is thrown.