Skip to content

Commit

Permalink
Fix compilation error for ranges with ADL begin/end (#2807)
Browse files Browse the repository at this point in the history
* Use `range_begin`/`end` to get formatted range iterators

* Add test for adl `begin`/`end`

* Apply clang-format

* Simplify tests
  • Loading branch information
rbrugo authored Mar 11, 2022
1 parent f6bcb25 commit a8fe8be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ struct formatter<
auto out = ctx.out();
*out++ = prefix;
int i = 0;
auto it = std::begin(range);
auto end = std::end(range);
auto it = detail::range_begin(range);
auto end = detail::range_end(range);
for (; it != end; ++it) {
if (i > 0) out = detail::write_delimiter(out);
if (custom_specs_) {
Expand Down
19 changes: 19 additions & 0 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ TEST(ranges_test, format_set) {
"{\"one\", \"two\"}");
}

namespace adl {
struct box {
int value;
};

auto begin(const box& b) -> const int* {
return &b.value;
}

auto end(const box& b) -> const int* {
return &b.value + 1;
}
} // namespace adl

TEST(ranges_test, format_adl_begin_end) {
auto b = adl::box{42};
EXPECT_EQ(fmt::format("{}", b), "[42]");
}

TEST(ranges_test, format_pair) {
auto p = std::pair<int, float>(42, 1.5f);
EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)");
Expand Down

0 comments on commit a8fe8be

Please sign in to comment.