From: Jonathan Wakely Date: Wed, 28 Feb 2024 15:05:08 +0000 (+0000) Subject: libstdc++: Fix std::basic_format_arg::handle for BasicFormatters X-Git-Tag: basepoints/gcc-15~896 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02ca9d3f0c5d2b0255df28f021834dd67ad79bc2;p=thirdparty%2Fgcc.git libstdc++: Fix std::basic_format_arg::handle for BasicFormatters std::basic_format_arg::handle is supposed to format its value as const if that is valid, to reduce the number of instantiations of the formatter's format function. I made a silly typo so that it checks formattable_with not formattable_with, which breaks support for BasicFormatters i.e. ones that can only format non-const types. There's a static_assert in the handle constructor which is supposed to improve diagnostics for trying to format a const argument with a formatter that doesn't support it. That condition can't fail, because the std::basic_format_arg constructor is already constrained to check that the argument type is formattable. The static_assert can be removed. libstdc++-v3/ChangeLog: * include/std/format (basic_format_arg::handle::__maybe_const_t): Fix condition to check if const type is formattable. (basic_format_arg::handle::handle(T&)): Remove redundant static_assert. * testsuite/std/format/formatter/basic.cc: New test. --- diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 961441e355b3..ee189f9086ce 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3210,7 +3210,7 @@ namespace __format // Format as const if possible, to reduce instantiations. template using __maybe_const_t - = __conditional_t<__formattable<_Tp>, const _Tp, _Tp>; + = __conditional_t<__formattable, const _Tp, _Tp>; template static void @@ -3228,10 +3228,6 @@ namespace __format explicit handle(_Tp& __val) noexcept { - if constexpr (!__formattable) - static_assert(!is_const_v<_Tp>, "std::format argument must be " - "non-const for this type"); - this->_M_ptr = __builtin_addressof(__val); auto __func = _S_format<__maybe_const_t<_Tp>>; this->_M_func = reinterpret_cast(__func); diff --git a/libstdc++-v3/testsuite/std/format/formatter/basic.cc b/libstdc++-v3/testsuite/std/format/formatter/basic.cc new file mode 100644 index 000000000000..56c18864135a --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/formatter/basic.cc @@ -0,0 +1,24 @@ +// { dg-do compile { target c++20 } } + +// BasicFormatter requirements do not require a const parameter. + +#include + +struct X { }; + +template<> struct std::formatter +{ + constexpr auto parse(format_parse_context& ctx) + { return ctx.begin(); } + + // Takes non-const X& + format_context::iterator format(X&, format_context& ctx) const + { + auto out = ctx.out(); + *out++ = 'x'; + return out; + } +}; + +X x; +auto s = std::format("{}", x);