Fix recursion check in range formatting

This commit is contained in:
Victor Zverovich 2023-03-04 08:03:00 -08:00
parent b94e1016fa
commit e0748e61dd
2 changed files with 17 additions and 11 deletions

View File

@ -505,13 +505,14 @@ struct range_formatter<
enum class range_format { disabled, map, set, sequence, string, debug_string };
namespace detail {
template <typename T> struct range_format_kind_ {
static constexpr auto value = std::is_same<range_reference_type<T>, T>::value
template <typename T>
struct range_format_kind_
: std::integral_constant<range_format,
std::is_same<uncvref_type<T>, T>::value
? range_format::disabled
: is_map<T>::value ? range_format::map
: is_set<T>::value ? range_format::set
: range_format::sequence;
};
: range_format::sequence> {};
template <range_format K, typename R, typename Char, typename Enable = void>
struct range_default_formatter;

View File

@ -179,15 +179,20 @@ TEST(ranges_test, format_to) {
EXPECT_STREQ(buf, "[1, 2, 3]");
}
struct path_like {
template <typename Char> struct path_like {
const path_like* begin() const;
const path_like* end() const;
operator std::string() const;
operator std::basic_string<Char>() const;
};
TEST(ranges_test, path_like) {
EXPECT_FALSE((fmt::is_range<path_like, char>::value));
TEST(ranges_test, disabled_range_formatting_of_path) {
// Range formatting of path is disabled because of infinite recursion
// (path element is itself a path).
EXPECT_EQ((fmt::range_format_kind<path_like<char>, char>::value),
fmt::range_format::disabled);
EXPECT_EQ((fmt::range_format_kind<path_like<wchar_t>, char>::value),
fmt::range_format::disabled);
}
// A range that provides non-const only begin()/end() to test fmt::join