Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage with std::generator #4057

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ struct range_formatter<
auto out = ctx.out();
auto it = detail::range_begin(range);
auto end = detail::range_end(range);
if (is_debug) return write_debug_string(out, it, end);
if (is_debug) return write_debug_string(out, std::move(it), end);

out = detail::copy<Char>(opening_bracket_, out);
int i = 0;
Expand Down
32 changes: 22 additions & 10 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -752,24 +752,36 @@ TEST(ranges_test, std_istream_iterator_join) {
EXPECT_EQ("1, 2, 3, 4, 5", fmt::format("{}", fmt::join(first, last, ", ")));
}

TEST(ranges_test, movable_only_istream_iter_join) {
// Mirrors C++20 std::ranges::basic_istream_view::iterator.
struct noncopyable_istream_iterator : std::istream_iterator<int> {
explicit noncopyable_istream_iterator(std::istringstream& iss)
: std::istream_iterator<int>{iss} {}
noncopyable_istream_iterator(const noncopyable_istream_iterator&) = delete;
noncopyable_istream_iterator(noncopyable_istream_iterator&&) = default;
};
static_assert(
!std::is_copy_constructible<noncopyable_istream_iterator>::value, "");
// Mirrors C++20 std::ranges::basic_istream_view::iterator.
struct noncopyable_istream_iterator : std::istream_iterator<int> {
using base = std::istream_iterator<int>;
explicit noncopyable_istream_iterator(std::istringstream& iss) : base{iss} {}
noncopyable_istream_iterator(const noncopyable_istream_iterator&) = delete;
noncopyable_istream_iterator(noncopyable_istream_iterator&&) = default;
};
static_assert(!std::is_copy_constructible<noncopyable_istream_iterator>::value,
"");

TEST(ranges_test, movable_only_istream_iter_join) {
auto&& iss = std::istringstream("1 2 3 4 5");
auto first = noncopyable_istream_iterator(iss);
auto last = std::istream_iterator<int>();
EXPECT_EQ("1, 2, 3, 4, 5",
fmt::format("{}", fmt::join(std::move(first), last, ", ")));
}

struct movable_iter_range {
std::istringstream iss{"1 2 3 4 5"};
noncopyable_istream_iterator begin() {
return noncopyable_istream_iterator{iss};
}
std::istream_iterator<int> end() { return {}; }
};

TEST(ranges_test, movable_only_istream_iter_join2) {
EXPECT_EQ("[1, 2, 3, 4, 5]", fmt::format("{}", movable_iter_range{}));
}

struct not_range {
void begin() const {}
void end() const {}
Expand Down