C++ Logo

sg16

Advanced search

LWG 3944: Formatters converting sequences of char to sequences of wchar_t

From: Mark de Wever <koraq_at_[hidden]>
Date: Mon, 24 Jul 2023 19:42:51 +0200
Based on the feedback last meeting I've written a proposed resolution
for LWG 3944: Formatters converting sequences of char to sequences of wchar_t.

We didn't take a vote, but my impression was that we only wanted to
prohibit the narrow char string to wide char range conversion and not
the other narrow char to wide char conversions. As mentioned in the LWG
issue I prefer avoid non-problematic types (const) char* too. This
prevents them from accidentally becoming a problem if (const) char*
becomes a range in a future version of C++.

Below the proposed wording based on N4950

[Drafting note: The unwanted conversion happens due to the formatter
specialization ([format.range.fmtdef])
  struct range-default-formatter<range_format::sequence, R, charT>
which is defined the format header. Therefore the disabling is only
needed in the format header.) -- end drafting note]

Modify [format.formatter.spec]

2.4 For each charT, the pointer type specializations
      template<> struct formatter<nullptr_t, charT>;
      template<> struct formatter<void*, charT>;
      template<> struct formatter<const void*, charT>;

 The parse member functions of these formatters interpret the format
 specification as a std-format-spec as described in
    [format.string.std].

   [Note 1: Specializations such as formatter<wchar_t, char>
   <del>and formatter<const char*, wchar_t</del>> that would require
   implicit multibyte / wide string or character conversion are
   disabled. — end note]

<ins>
3 The header <format> provides the following disabled specializations:
3.1 The string type specializations
      template<> struct formatter<char*, wchar_t>;
      template<> struct formatter<const char*, wchar_t>;
      template<size_t N> struct formatter<char[N], wchar_t>;
      template<class traits, class Allocator>
        struct formatter<basic_string<char, traits, Allocator>, wchar_t>;
      template<class traits>
      struct formatter<basic_string_view<char, traits>, wchar_t>;
</ins>

Cheers,
Mark

Received on 2023-07-24 17:42:57