public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "iamsupermouse at mail dot ru" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/112832] New: [std::format] Broken non-SFINAE-friendly `set_debug_format()` for `const char *` formatter
Date: Sun, 03 Dec 2023 16:53:12 +0000	[thread overview]
Message-ID: <bug-112832-4@http.gcc.gnu.org/bugzilla/> (raw)

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
    }

             reply	other threads:[~2023-12-03 16:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-03 16:53 iamsupermouse at mail dot ru [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-112832-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).