public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/112832] New: [std::format] Broken non-SFINAE-friendly `set_debug_format()` for `const char *` formatter
@ 2023-12-03 16:53 iamsupermouse at mail dot ru
  2023-12-04 11:36 ` [Bug libstdc++/112832] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: iamsupermouse at mail dot ru @ 2023-12-03 16:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112832
           Summary: [std::format] Broken non-SFINAE-friendly
                    `set_debug_format()` for `const char *` formatter
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: iamsupermouse at mail dot ru
  Target Milestone: ---

GCC doesn't seem to implement `{:?}` debug format for strings yet.

Yet for some reason `std::formatter<const char *>` has a `set_debug_format()`
function, which causes a hard error when used (isn't SFINAE-friendly).

`set_debug_format()` should be SFINAE-friendly, to allow the user to call it
conditionally when available.

Here's a code sample that fails to compile because of it:
https://gcc.godbolt.org/z/PbfnsxE9n (still fails in GCC 14 trunk)

    #include <format>
    #include <iostream>
    #include <string>
    #include <type_traits>
    #include <utility>

    template <typename T>
    struct PreferDebugFormat
    {
        T &target;
        constexpr PreferDebugFormat(T &target) : target(target) {}
    };
    template <typename T>
    PreferDebugFormat(T &&) -> PreferDebugFormat<std::remove_reference_t<T> &>;

    template <typename T, typename CharT>
    struct std::formatter<PreferDebugFormat<T>, CharT> :
std::formatter<std::remove_cvref_t<T>, CharT>
    {
        using base = std::formatter<std::remove_cvref_t<T>, CharT>;

        constexpr decltype(auto) parse(std::basic_format_parse_context<CharT>
&parse_ctx)
        {
            if constexpr (requires{this->set_debug_format();})
                this->set_debug_format();
            return base::parse(parse_ctx);
        }

        template <typename OutputIt>
        constexpr decltype(auto) format(const PreferDebugFormat<T> &arg,
std::basic_format_context<OutputIt, CharT> &format_ctx) const
        {
            return base::format(arg.target, format_ctx);
        }
    };

    template <typename T>
    std::string ToDebugString(const T &value)
    {
        return std::format("{}", PreferDebugFormat(value));
    }

    int main()
    {
        std::cout << ToDebugString("foo\nbar") << '\n'; // hard error == BUG
        std::cout << ToDebugString(std::string_view("foo\nbar")) << '\n'; //
ok, no debug format yet
        std::cout << ToDebugString(42) << '\n'; // ok, no debug format
    }

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-06 14:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-03 16:53 [Bug libstdc++/112832] New: [std::format] Broken non-SFINAE-friendly `set_debug_format()` for `const char *` formatter iamsupermouse at mail dot ru
2023-12-04 11:36 ` [Bug libstdc++/112832] " redi at gcc dot gnu.org
2023-12-05 16:49 ` cvs-commit at gcc dot gnu.org
2023-12-06 14:44 ` cvs-commit at gcc dot gnu.org
2023-12-06 14:45 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).