microsoft / microsoft/STL

<format>: `charT` for `std::formatter<basic_string_view>` is not supposed to be template parameter

Open
#5,278 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

format LWG issue needed
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

https://eel.is/c++draft/format.formatter.spec#2 says

Let charT be either char or wchar_t. ...
...

  • For each charT, the debug-enabled string type specializations
    ...
    template<class traits>
    struct formatter<basic_string_view<charT, traits>, charT>;
    

meaning the two specializations should be written separately

template<class traits>
struct formatter<basic_string_view<char, traits>, char>;

template<class traits>
struct formatter<basic_string_view<wchar_t, traits>, wchar_t>;

but they are implemented as https://github.com/microsoft/STL/blob/d43d49a3fdfa9d4fdbafd82dc517250b93269c94/stl/inc/__msvc_formatter.hpp#L131-L132 https://github.com/microsoft/STL/blob/d43d49a3fdfa9d4fdbafd82dc517250b93269c94/stl/inc/__msvc_formatter.hpp#L276-L284

This may cause trouble if users add templated specializations but basic_string_view is not excluded from its constraint, but only supporting char (Godbolt link):

#include <format>
#include <string_view>
#include <concepts>

struct X {};

template<class T>
concept C = std::same_as<T, X> || std::same_as<T, std::string_view>;

template<C T>
struct std::formatter<T>;

std::formatter<std::string_view> f{};

Then the specialization is ambiguous

<source>(13): error C2752: 'std::formatter<std::string_view,char>': more than one partial specialization matches the template argument list
Z:/compilers/msvc/14.41.33923-14.41.33923.0/include\__msvc_formatter.hpp(261): note: could be 'std::formatter<std::basic_string_view<_CharT,_Traits>,_CharT>'
<source>(11): note: or       'std::formatter<T,char>'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the formatter specializations in stl/inc/__msvc_formatter.hpp at lines 131-132 and 276-284, then compare them with the format.formatter.spec wording linked in the issue. Reproduce the ambiguity using the supplied Godbolt example. Done means the basic_string_view specializations match the standard and the example no longer reports competing partial specializations.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.