// { dg-do run { target c++23 } } // { dg-timeout-factor 2 } #include #include #include #include template bool is_format_string_for(const char* str, Args&&... args) { try { (void) std::vformat(str, std::make_format_args(args...)); return true; } catch (const std::format_error&) { return false; } } #define WIDEN_(C, S) ::std::__format::_Widen(S, L##S) #define WIDEN(S) WIDEN_(_CharT, S) template class Adaptor> void test_format_string() { Adaptor q; VERIFY( !is_format_string_for("{:?}", q) ); VERIFY( !is_format_string_for("{:P}", q) ); // width needs to be integer type VERIFY( !is_format_string_for("{:{}}", q, 1.0f) ); } struct NoFormat { friend auto operator<=>(NoFormat, NoFormat) = default; }; struct MutFormat { MutFormat() = default; MutFormat(int p) : x(p) {} int x; friend auto operator<=>(MutFormat, MutFormat) = default; }; template struct std::formatter : std::formatter { template Out format(MutFormat& mf, basic_format_context& ctx) const { return std::formatter::format(mf.x, ctx); } }; template struct NotFormattableCont : std::vector { using std::vector::vector; }; template constexpr auto std::format_kind> = std::range_format::disabled; template> class Adaptor> void test_output() { const std::vector v{3, 2, 1}; std::basic_string<_CharT> res; Adaptor> q(std::from_range, v); res = std::format(WIDEN("{}"), q); VERIFY( res == WIDEN("[3, 2, 1]") ); res = std::format(WIDEN("{}"), std::as_const(q)); VERIFY( res == WIDEN("[3, 2, 1]") ); res = std::format(WIDEN("{:n:#x}"), q); VERIFY( res == WIDEN("0x3, 0x2, 0x1") ); res = std::format(WIDEN("{:=^23:#04x}"), q); VERIFY( res == WIDEN("==[0x03, 0x02, 0x01]===") ); // Sequence output is always used std::queue<_CharT, std::basic_string<_CharT>> qs( std::from_range, std::basic_string_view<_CharT>(WIDEN("321"))); res = std::format(WIDEN("{}"), qs); VERIFY( res == WIDEN("['3', '2', '1']") ); res = std::format(WIDEN("{::}"), std::as_const(qs)); VERIFY( res == WIDEN("[3, 2, 1]") ); res = std::format(WIDEN("{:?s}"), qs); VERIFY( res == WIDEN(R"("321")") ); Adaptor> qd(std::from_range, v); res = std::format(WIDEN("{}"), qd); VERIFY( res == WIDEN("[3, 2, 1]") ); res = std::format(WIDEN("{}"), std::as_const(qd)); VERIFY( res == WIDEN("[3, 2, 1]") ); Adaptor mq(std::from_range, v); res = std::format(WIDEN("{}"), mq); VERIFY( res == WIDEN("[3, 2, 1]") ); static_assert(!std::formattable, _CharT>); static_assert(!std::formattable, _CharT>); static_assert(!std::formattable, _CharT>); // Formatter check if container is formattable, not container elements. static_assert(!std::formattable>, _CharT>); } template> class Adaptor> void test_adaptor() { test_format_string(); test_output(); test_output(); static_assert(!std::formattable, int>); static_assert(!std::formattable, char32_t>); } template void test_compare() { const std::vector v{3, 2, 1}; std::basic_string<_CharT> res; std::priority_queue, std::greater<>> q( std::from_range, v); res = std::format(WIDEN("{}"), q); VERIFY( res == WIDEN("[1, 2, 3]") ); } int main() { test_adaptor(); test_adaptor(); test_compare(); }