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 compilation error for ranges with ADL begin/end #2807

Merged
merged 4 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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
23 changes: 23 additions & 0 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ TEST(ranges_test, format_set) {
"{\"one\", \"two\"}");
}

namespace adl {
struct box {
int value;
};

auto begin(box& b) noexcept -> int* { return std::addressof(b.value); }

auto end(box& b) noexcept -> int* { return std::addressof(b.value) + 1; }
rbrugo marked this conversation as resolved.
Show resolved Hide resolved

auto begin(const box& b) noexcept -> const int* {
return std::addressof(b.value);
}
rbrugo marked this conversation as resolved.
Show resolved Hide resolved

auto end(const box& b) noexcept -> const int* {
return std::addressof(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