| 1 | // { dg-do run { target c++20 } } |
| 2 | |
| 3 | #include <format> |
| 4 | #include <string> |
| 5 | #include <iterator> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | template<typename C> |
| 9 | struct move_only_iterator |
| 10 | { |
| 11 | using iterator = std::back_insert_iterator<std::basic_string<C>>; |
| 12 | using value_type = iterator::value_type; |
| 13 | using difference_type = iterator::difference_type; |
| 14 | using iterator_category = std::output_iterator_tag; |
| 15 | |
| 16 | move_only_iterator(iterator b) : base_(b) { } |
| 17 | move_only_iterator(move_only_iterator&&) = default; |
| 18 | move_only_iterator& operator=(move_only_iterator&&) = default; |
| 19 | |
| 20 | move_only_iterator& operator++() { ++base_; return *this; } |
| 21 | move_only_iterator operator++(int) { auto tmp = *this; ++base_; return tmp; } |
| 22 | |
| 23 | decltype(auto) operator*() { return *base_; } |
| 24 | |
| 25 | private: |
| 26 | iterator base_; |
| 27 | }; |
| 28 | |
| 29 | void |
| 30 | test_move_only() |
| 31 | { |
| 32 | const char arg1[] = "matte"; |
| 33 | int arg2 = '!'; |
| 34 | |
| 35 | std::string str; |
| 36 | move_only_iterator<char> mo(std::back_inserter(str)); |
| 37 | auto res = std::vformat_to(std::move(mo), "for{:.3} that{:c}", |
| 38 | std::make_format_args(arg1, arg2)); |
| 39 | static_assert(std::is_same_v<decltype(res), decltype(mo)>); |
| 40 | VERIFY( str == "format that!" ); |
| 41 | |
| 42 | const wchar_t warg1[] = L"matte"; |
| 43 | long warg2 = L'!'; |
| 44 | std::wstring wstr; |
| 45 | move_only_iterator<wchar_t> wmo(std::back_inserter(wstr)); |
| 46 | auto wres = std::vformat_to(std::move(wmo), L"for{:.3} that{:c}", |
| 47 | std::make_wformat_args(warg1, warg2)); |
| 48 | static_assert(std::is_same_v<decltype(wres), decltype(wmo)>); |
| 49 | VERIFY( wstr == L"format that!" ); |
| 50 | } |
| 51 | |
| 52 | int main() |
| 53 | { |
| 54 | test_move_only(); |
| 55 | } |