diff --git a/include/fmt/format.h b/include/fmt/format.h index 44c2a3b4..065aa496 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -469,15 +469,17 @@ template class truncating_iterator_base { protected: OutputIt out_; size_t limit_; - size_t count_; + size_t count_ = 0; + truncating_iterator_base() : out_(), limit_(0) {} + truncating_iterator_base(OutputIt out, size_t limit) - : out_(out), limit_(limit), count_(0) {} + : out_(out), limit_(limit) {} public: using iterator_category = std::output_iterator_tag; using value_type = typename std::iterator_traits::value_type; - using difference_type = void; + using difference_type = std::ptrdiff_t; using pointer = void; using reference = void; using _Unchecked_type = @@ -502,6 +504,8 @@ class truncating_iterator public: using value_type = typename truncating_iterator_base::value_type; + truncating_iterator() = default; + truncating_iterator(OutputIt out, size_t limit) : truncating_iterator_base(out, limit) {} @@ -525,6 +529,8 @@ template class truncating_iterator : public truncating_iterator_base { public: + truncating_iterator() = default; + truncating_iterator(OutputIt out, size_t limit) : truncating_iterator_base(out, limit) {} diff --git a/test/format-test.cc b/test/format-test.cc index 34b4ca48..a71aeaf1 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -12,9 +12,11 @@ #include #include #include +#include #include #include #include +#include // Check if fmt/format.h compiles with windows.h included before it. #ifdef _WIN32 @@ -157,6 +159,24 @@ TEST(IteratorTest, TruncatingIterator) { EXPECT_EQ(it.base(), p + 1); } + +TEST(IteratorTest, TruncatingIteratorDefaultConstruct) { + static_assert( + std::is_default_constructible>::value, + ""); + + fmt::detail::truncating_iterator it; + EXPECT_EQ(nullptr, it.base()); + EXPECT_EQ(std::size_t{0}, it.count()); +} + +#ifdef __cpp_lib_ranges +TEST(IteratorTest, TruncatingIteratorOutputIterator) { + static_assert(std::output_iterator, + char>); +} +#endif + TEST(IteratorTest, TruncatingBackInserter) { std::string buffer; auto bi = std::back_inserter(buffer);