https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112607

            Bug ID: 112607
           Summary: <format>: _Normalize does not consider char_type for
                    the basic_string_view case
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

When T in basic_format_arg(T& v) is a specialization of basic_string_view or
basic_string, format#arg-6.8 indicates: 

otherwise, if TD is a specialization of basic_string_view or basic_string and
TD​::​value_type is char_type, initializes value with
basic_string_view<char_type>(v.data(), v.size());

We need to consider TD​::​value_type is char_type.

However, libstd++ only uses __is_specialization_of to detect whether T is a
specialization of basic_string_view or basic_string (format#L3118-L3121):

        else if constexpr (__is_specialization_of<_Td, basic_string_view>)
          return type_identity<basic_string_view<_CharT>>();
        else if constexpr (__is_specialization_of<_Td, basic_string>)
          return type_identity<basic_string_view<_CharT>>();

This causes basic_format_arg to incorrectly use wstring_view to initialize
string_view when customizing std::wstring.

https://godbolt.org/z/6Kd16z8qK

   #include <format>

   template<>
   struct std::formatter<std::wstring> : std::formatter<std::string> {
     auto format(const std::wstring& obj, auto& ctx) const {
     return std::formatter<std::string>::format(" ", ctx);
   }
  };

  int main(){
    std::wstring wstr;
    std::string str = std::format("{}", wstr);
  }

Reply via email to