Skip to content

Commit

Permalink
fix ambiguous formatter lookup for flat_set (fmtlib#3561)
Browse files Browse the repository at this point in the history
  • Loading branch information
5chmidti authored Jul 31, 2023
1 parent eacd51c commit 8a4bec5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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
30 changes: 30 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,35 @@ 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>;

using iterator = typename std::vector<T>::iterator;
using const_iterator = typename std::vector<T>::const_iterator;

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

iterator begin() { return c.begin(); }
const_iterator begin() const { return c.begin(); }

iterator end() { return c.end(); }
const_iterator end() const { 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

0 comments on commit 8a4bec5

Please sign in to comment.