From a8fe8becf45b520c03250bda6e6914786ee91d83 Mon Sep 17 00:00:00 2001 From: Riccardo Brugo Date: Fri, 11 Mar 2022 20:36:39 +0100 Subject: [PATCH] Fix compilation error for ranges with ADL `begin`/`end` (#2807) * Use `range_begin`/`end` to get formatted range iterators * Add test for adl `begin`/`end` * Apply clang-format * Simplify tests --- include/fmt/ranges.h | 4 ++-- test/ranges-test.cc | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 77089f36..edff8f96 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -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_) { diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 8509ee79..aa910e79 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -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(42, 1.5f); EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)");