3701. Make formatter<remove_cvref_t<const charT[N]>, charT> requirement explicit

Section: 28.5.6.4 [format.formatter.spec] Status: C++23 Submitter: Mark de Wever Opened: 2022-05-17 Last modified: 2023-11-22

Priority: Not Prioritized

View other active issues in [format.formatter.spec].

View all other issues in [format.formatter.spec].

View all issues with C++23 status.

Discussion:

The wording in 28.5.5 [format.functions]/20 and 28.5.5 [format.functions]/25 both contain

formatter<remove_cvref_t<Ti>, charT> meets the BasicFormatter requirements (28.5.6.1 [formatter.requirements]) for each Ti in Args.

The issue is that remove_cvref_t<const charT[N]> becomes charT[N]. 28.5.6.4 [format.formatter.spec]/2.2 requires a specialization for

template<size_t N> struct formatter<const charT[N], charT>;

but there's no requirement to provide

 template<size_t N> struct formatter<charT[N], charT>;

There's no wording preventing library vendors from providing additional specializations. So it's possible to implement the current specification but the indirect requirement is odd. I noticed this while implementing a formattable concept. The concept is based on the formattable concept of P2286 "Formatting Ranges" (This paper is targeting C++23.)

It could be argued that the specialization

template<size_t N> struct formatter<const charT[N], charT>

is not needed and should be removed from the Standard. This will be an API break. Vendors can decide to keep the no longer required specialization as an extension; which would lead to implementation divergence. Microsoft is already shipping this specialization as stable and Victor doesn't like the removal too.

Therefore I only propose to add the required formatter specialization.

[2022-06-21; 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 28.5.6.4 [format.formatter.spec] as indicated:

    -2- Let charT be either char or wchar_t. Each specialization of formatter is either enabled or disabled, as described below. Each header that declares the template formatter provides the following enabled specializations:

    1. (2.1) — The specializations […]

    2. (2.2) — For each charT, the string type specializations

      template<> struct formatter<charT*, charT>;
      template<> struct formatter<const charT*, charT>;
      template<size_t N> struct formatter<charT[N], charT>;
      template<size_t N> struct formatter<const charT[N], charT>;
      template<class traits, class Allocator>
        struct formatter<basic_string<charT, traits, Allocator>, charT>;
      template<class traits>
        struct formatter<basic_string_view<charT, traits>, charT>;
      
    3. (2.3) — […]

    4. (2.4) — […]