diff --git a/include/fmt/format.h b/include/fmt/format.h index df387422..d99e57b8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -395,6 +395,7 @@ template class truncating_iterator_base { public: using iterator_category = std::output_iterator_tag; + using value_type = typename std::iterator_traits::value_type; using difference_type = void; using pointer = void; using reference = void; @@ -415,12 +416,10 @@ class truncating_iterator; template class truncating_iterator : public truncating_iterator_base { - using traits = std::iterator_traits; - - mutable typename traits::value_type blackhole_; + mutable typename truncating_iterator_base::value_type blackhole_; public: - using value_type = typename traits::value_type; + using value_type = typename truncating_iterator_base::value_type; truncating_iterator(OutputIt out, std::size_t limit) : truncating_iterator_base(out, limit) {} @@ -445,13 +444,11 @@ template class truncating_iterator : public truncating_iterator_base { public: - using value_type = typename OutputIt::container_type::value_type; - truncating_iterator(OutputIt out, std::size_t limit) : truncating_iterator_base(out, limit) {} - truncating_iterator& operator=(value_type val) { - if (this->count_++ < this->limit_) this->out_ = val; + template truncating_iterator& operator=(T val) { + if (this->count_++ < this->limit_) *this->out_++ = val; return *this; } diff --git a/test/format-test.cc b/test/format-test.cc index cf4e764d..76f57106 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2080,6 +2080,33 @@ TEST(FormatTest, WideFormatToN) { EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4)); } +struct test_output_iterator { + char* data; + + using iterator_category = std::output_iterator_tag; + using value_type = void; + using difference_type = void; + using pointer = void; + using reference = void; + + test_output_iterator& operator++() { + ++data; + return *this; + } + test_output_iterator operator++(int) { + auto tmp = *this; + ++data; + return tmp; + } + char& operator*() { return *data; } +}; + +TEST(FormatTest, FormatToNOutputIterator) { + char buf[10] = {}; + fmt::format_to_n(test_output_iterator{buf}, 10, "{}", 42); + EXPECT_STREQ(buf, "42"); +} + #if FMT_USE_CONSTEXPR struct test_arg_id_handler { enum result { NONE, EMPTY, INDEX, NAME, ERROR };