Skip to content

Commit

Permalink
Fix handling of output iterators in format_to_n (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 5, 2020
1 parent aa07c57 commit bdcf435
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,13 @@ template <typename OutputIt>
class truncating_iterator<OutputIt, std::true_type>
: public truncating_iterator_base<OutputIt> {
public:
using value_type = typename OutputIt::container_type::value_type;
using value_type = void;

truncating_iterator(OutputIt out, std::size_t limit)
: truncating_iterator_base<OutputIt>(out, limit) {}

truncating_iterator& operator=(value_type val) {
if (this->count_++ < this->limit_) this->out_ = val;
template <typename T> truncating_iterator& operator=(T val) {
if (this->count_++ < this->limit_) *this->out_++ = val;
return *this;
}

Expand Down
27 changes: 27 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit bdcf435

Please sign in to comment.