Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SainoNamkho opened this issue Feb 12, 2025 · 2 comments
Labels
format C++20/23 format LWG issue needed A wording defect that should be submitted to LWG as a new issue

Comments

@SainoNamkho
Copy link

SainoNamkho commented Feb 12, 2025

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

template <class _CharT>
concept _Format_supported_charT = _Is_any_of_v<_CharT, char, wchar_t>;
template <_Format_supported_charT _CharT, class _Traits>
struct formatter<basic_string_view<_CharT, _Traits>, _CharT>
: _Formatter_base<basic_string_view<_CharT, _Traits>, _CharT, _Basic_format_arg_type::_String_type> {
#if _HAS_CXX23
constexpr void set_debug_format() noexcept {
this->_Set_debug_format();
}
#endif // _HAS_CXX23
};

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>'
@SainoNamkho SainoNamkho changed the title <format>: charT for std::formatter<basic_string_view> is not to be template parameter <format>: charT for std::formatter<basic_string_view> is not supposed to be template parameter Feb 12, 2025
SainoNamkho added a commit to SainoNamkho/STL that referenced this issue Feb 12, 2025
@SainoNamkho SainoNamkho mentioned this issue Feb 12, 2025
@davidmrdavid davidmrdavid added the LWG issue needed A wording defect that should be submitted to LWG as a new issue label Feb 12, 2025
@davidmrdavid
Copy link
Member

We discussed this on today's maintainer sync and we think the standard is unclear on the interplay between concepts and WG21-N5001 [namespace.std]/2. We'll return to this issue after some review.

At first glance, we feel the specialization should not be permitted as it stomps on std::formatter<std::string_view>.

@davidmrdavid davidmrdavid added the format C++20/23 format label Feb 12, 2025
@frederick-vs-ja
Copy link
Contributor

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
    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>;

I don't think anything in that paragraph disallows merging specializations for char and wchar_t. libc++ also merges these specializations. libstdc++ merges some but not all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format C++20/23 format LWG issue needed A wording defect that should be submitted to LWG as a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants