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 ⌖
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
}