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 ambiguous formatter lookup for flat_set #3561

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,11 @@ template <typename Container> struct all {
} // namespace detail

template <typename T, typename Char>
struct formatter<T, Char,
enable_if_t<detail::is_container_adaptor_like<T>::value>>
struct formatter<
T, Char,
enable_if_t<conjunction<detail::is_container_adaptor_like<T>,
bool_constant<range_format_kind<T, Char>::value ==
range_format::disabled>>::value>>
: formatter<detail::all<typename T::container_type>, Char> {
using all = detail::all<typename T::container_type>;
template <typename FormatContext>
Expand Down
27 changes: 27 additions & 0 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include "gtest/gtest.h"
Expand Down Expand Up @@ -80,6 +81,32 @@ TEST(ranges_test, format_set) {
"{\"one\", \"two\"}");
}

// Models std::flat_set close enough to test if no ambiguous lookup of a
// formatter happens due to the flat_set type matching is_set and
// is_container_adaptor_like
template <typename T> class flat_set {
public:
using key_type = T;
using container_type = std::vector<T>;

template <typename... Ts>
explicit flat_set(Ts&&... args) : c{std::forward<Ts>(args)...} {}

auto begin() -> decltype(auto) { return c.begin(); }
auto begin() const -> decltype(auto) { return c.begin(); }

auto end() -> decltype(auto) { return c.end(); }
auto end() const -> decltype(auto) { return c.end(); }

private:
std::vector<T> c;
};

TEST(ranges_test, format_flat_set) {
EXPECT_EQ(fmt::format("{}", flat_set<std::string>{"one", "two"}),
"{\"one\", \"two\"}");
}

namespace adl {
struct box {
int value;
Expand Down