// { dg-do run { target c++23 } } #include #include #include #include #define WIDEN_(C, S) ::std::__format::_Widen(S, L##S) #define WIDEN(S) WIDEN_(_CharT, S) template class Formatter = std::range_formatter> struct MyVector : std::vector { using std::vector::vector; }; template class Formatter, typename CharT> struct std::formatter, CharT> { constexpr formatter() noexcept { using _CharT = CharT; _formatter.set_brackets(WIDEN("<"), WIDEN(">")); _formatter.set_separator(WIDEN("; ")); } constexpr std::basic_format_parse_context::iterator parse(std::basic_format_parse_context& pc) { return _formatter.parse(pc); } template typename std::basic_format_context::iterator format(const MyVector& mv, std::basic_format_context& fc) const { return _formatter.format(mv, fc); } private: Formatter _formatter; }; template class Formatter> void test_default() { MyVector vec{1, 2, 3}; std::basic_string<_CharT> res; res = std::format(WIDEN("{}"), vec); VERIFY( res == WIDEN("<1; 2; 3>") ); res = std::format(WIDEN("{:}"), vec); VERIFY( res == WIDEN("<1; 2; 3>") ); res = std::format(WIDEN("{:n}"), vec); VERIFY( res == WIDEN("1; 2; 3") ); res = std::format(WIDEN("{:3}"), vec); VERIFY( res == WIDEN("<1; 2; 3>") ); res = std::format(WIDEN("{:10}"), vec); VERIFY( res == WIDEN("<1; 2; 3> ") ); res = std::format(WIDEN("{:{}}"), vec, 10); VERIFY( res == WIDEN("<1; 2; 3> ") ); res = std::format(WIDEN("{1:{0}}"), 10, vec); VERIFY( res == WIDEN("<1; 2; 3> ") ); res = std::format(WIDEN("{:10n}"), vec); VERIFY( res == WIDEN("1; 2; 3 ") ); res = std::format(WIDEN("{:*<11}"), vec); VERIFY( res == WIDEN("<1; 2; 3>**") ); res = std::format(WIDEN("{:->12}"), vec); VERIFY( res == WIDEN("---<1; 2; 3>") ); res = std::format(WIDEN("{:=^13}"), vec); VERIFY( res == WIDEN("==<1; 2; 3>==") ); res = std::format(WIDEN("{:=^13n}"), vec); VERIFY( res == WIDEN("===1; 2; 3===") ); res = std::format(WIDEN("{::#x}"), vec); VERIFY( res == WIDEN("<0x1; 0x2; 0x3>") ); res = std::format(WIDEN("{:|^25n:#05x}"), vec); VERIFY( res == WIDEN("|||0x001; 0x002; 0x003|||") ); // ':' is start of the format string for element res = std::format(WIDEN("{::^+4}"), vec); VERIFY( res == WIDEN("< +1 ; +2 ; +3 >") ); } template class Formatter> void test_override() { MyVector<_CharT, Formatter> vc{'a', 'b', 'c', 'd'}; MyVector, Formatter> vp{{1, 11}, {2, 21}}; std::basic_string<_CharT> res; res = std::format(WIDEN("{:s}"), vc); VERIFY( res == WIDEN("abcd") ); res = std::format(WIDEN("{:?s}"), vc); VERIFY( res == WIDEN("\"abcd\"") ); res = std::format(WIDEN("{:+^6s}"), vc); VERIFY( res == WIDEN("+abcd+") ); res = std::format(WIDEN("{:m}"), vp); VERIFY( res == WIDEN("{1: 11, 2: 21}") ); res = std::format(WIDEN("{:=^20m}"), vp); VERIFY( res == WIDEN("==={1: 11, 2: 21}===") ); } template class Formatter> void test_outputs() { test_default(); test_default(); test_override(); test_override(); } void test_nested() { MyVector> v { {1, 2}, {11, 12} }; std::string res = std::format("{}", v); VERIFY( res == "<<1; 2>; <11; 12>>" ); res = std::format("{:+^18:n:02}", v); VERIFY( res == "+<01; 02; 11; 12>+" ); } struct MyFlatMap : std::flat_map { using std::flat_map::flat_map; }; template struct std::formatter // This cannot apply format BitVector const&, because formatted type would // be std::pair, and formatter for // pair cannot format it. : std::range_formatter {}; void test_const_ref_type_mismatch() { MyFlatMap m{{1, 11}, {2, 22}}; std::string res = std::format("{:m}", m); VERIFY( res == "{1: 11, 2: 22}" ); } template using VectorFormatter = std::formatter, CharT>; int main() { test_outputs(); test_outputs(); test_nested(); test_const_ref_type_mismatch(); }