diff --git a/include/fmt/format.h b/include/fmt/format.h index 029413f4..6cab8386 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -443,13 +443,17 @@ class counting_iterator { ++count_; return *this; } - counting_iterator operator++(int) { auto it = *this; ++*this; return it; } + friend counting_iterator operator+(counting_iterator it, difference_type n) { + it.count_ += static_cast(n); + return it; + } + value_type operator*() const { return {}; } }; @@ -583,6 +587,12 @@ OutputIt copy_str(InputIt begin, InputIt end, OutputIt it) { [](char c) { return static_cast(c); }); } +template +inline counting_iterator copy_str(InputIt begin, InputIt end, + counting_iterator it) { + return it + (end - begin); +} + #ifndef FMT_USE_GRISU # define FMT_USE_GRISU 1 #endif diff --git a/test/format-test.cc b/test/format-test.cc index 7870e9d0..877d2300 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -147,6 +147,7 @@ TEST(IteratorTest, CountingIterator) { auto prev = it++; EXPECT_EQ(prev.count(), 0); EXPECT_EQ(it.count(), 1); + EXPECT_EQ((it + 41).count(), 42); } TEST(IteratorTest, TruncatingIterator) {