Title
basic_format_arg mis-handles basic_string_view with custom traits
Status
c++23
Section
[format.arg]
Submitter
Casey Carter

Created on 2021-04-20.00:00:00 last changed 5 months ago

Messages

Date: 2021-06-07.16:58:04

Proposed resolution:

This wording is relative to N4885.

  1. Modify [format.arg] as indicated:

    template<class traits>
      explicit basic_format_arg(basic_string_view<char_type, traits> s) noexcept;
    

    -9- Effects: Initializes value with sbasic_string_view<char_type>(s.data(), s.size()).

Date: 2021-06-07.00:00:00

[ 2021-06-07 Approved at June 2021 virtual plenary. Status changed: Voting → WP. ]

Date: 2021-05-15.00:00:00

[ 2021-05-10; Reflector poll ]

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

Date: 2021-04-20.00:00:00

basic_format_arg has a constructor that accepts a basic_string_view of an appropriate character type, with any traits type. The constructor is specified in [format.arg] as:

template<class traits>
explicit basic_format_arg(basic_string_view<char_type, traits> s) noexcept;

-9- Effects: Initializes value with s.

Recall that value is a variant<monostate, bool, char_type, int, unsigned int, long long int, unsigned long long int, float, double, long double, const char_type*, basic_string_view<char_type>, const void*, handle> as specified earlier in the subclause. Since basic_string_view<meow, woof> cannot be initialized with an lvalue basic_string_view<meow, quack> — and certainly none of the other alternative types can be initialized by such an lvalue — the effects of this constructor are ill-formed when traits is not std::char_traits<char_type>.

The basic_string constructor deals with this same issue by ignoring the deduced traits type when initializing value's basic_string_view member:

template<class traits, class Allocator>
  explicit basic_format_arg(
    const basic_string<char_type, traits, Allocator>& s) noexcept;

-10- Effects: Initializes value with basic_string_view<char_type>(s.data(), s.size()).

which immediately begs the question of "why doesn't the basic_string_view constructor do the same?"

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2021-06-07 16:58:04adminsetmessages: + msg11907
2021-06-07 16:58:04adminsetstatus: voting -> wp
2021-05-26 21:11:22adminsetstatus: ready -> voting
2021-05-10 16:28:00adminsetmessages: + msg11794
2021-05-10 16:28:00adminsetstatus: new -> ready
2021-04-24 15:51:44adminsetstatus: ready -> new
2021-04-24 15:45:29adminsetmessages: + msg11779
2021-04-20 00:00:00admincreate