mirror of
https://github.com/fmtlib/fmt.git
synced 2026-02-15 06:39:58 +08:00
Fix range suppresor formatter (#4660)
This commit is contained in:
parent
cf74caae38
commit
5cc5072aa6
@ -746,7 +746,6 @@ struct formatter<tuple_join_view<Tuple, Char>, Char,
|
|||||||
return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
|
return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
// Check if T has an interface like a container adaptor (e.g. std::stack,
|
// Check if T has an interface like a container adaptor (e.g. std::stack,
|
||||||
// std::queue, std::priority_queue).
|
// std::queue, std::priority_queue).
|
||||||
@ -766,10 +765,19 @@ template <typename Container> struct all {
|
|||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns "true" if "T" is a container adaptor (like "std::stack")
|
||||||
|
* that should be formatted by iterating over the underlying container.
|
||||||
|
* */
|
||||||
|
|
||||||
|
FMT_EXPORT
|
||||||
|
template <typename T>
|
||||||
|
struct is_container_adaptor : detail::is_container_adaptor_like<T> {};
|
||||||
|
|
||||||
template <typename T, typename Char>
|
template <typename T, typename Char>
|
||||||
struct formatter<
|
struct formatter<
|
||||||
T, Char,
|
T, Char,
|
||||||
enable_if_t<conjunction<detail::is_container_adaptor_like<T>,
|
enable_if_t<conjunction<is_container_adaptor<T>,
|
||||||
bool_constant<range_format_kind<T, Char>::value ==
|
bool_constant<range_format_kind<T, Char>::value ==
|
||||||
range_format::disabled>>::value>>
|
range_format::disabled>>::value>>
|
||||||
: formatter<detail::all<typename T::container_type>, Char> {
|
: formatter<detail::all<typename T::container_type>, Char> {
|
||||||
|
|||||||
@ -797,3 +797,33 @@ struct not_range {
|
|||||||
void end() const {}
|
void end() const {}
|
||||||
};
|
};
|
||||||
static_assert(!fmt::is_formattable<not_range>{}, "");
|
static_assert(!fmt::is_formattable<not_range>{}, "");
|
||||||
|
|
||||||
|
namespace test_detail {
|
||||||
|
template <typename T>
|
||||||
|
struct partial_opt_out_wrapper {
|
||||||
|
using container_type = std::vector<T>;
|
||||||
|
std::vector<T> c = {1, 2, 3};
|
||||||
|
};
|
||||||
|
} // namespace test_detail
|
||||||
|
|
||||||
|
namespace fmt {
|
||||||
|
template <typename T>
|
||||||
|
struct is_container_adaptor<test_detail::partial_opt_out_wrapper<T>> : std::false_type {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct formatter<test_detail::partial_opt_out_wrapper<T>> {
|
||||||
|
constexpr fmt::format_parse_context::iterator parse(fmt::format_parse_context& ctx) const {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt::format_context::iterator format(const test_detail::partial_opt_out_wrapper<T>& val,
|
||||||
|
fmt::format_context& ctx) const {
|
||||||
|
return fmt::format_to(ctx.out(), "PartialOptOut(size={})", val.c.size());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace fmt
|
||||||
|
|
||||||
|
TEST(ranges_test, container_adaptor_partial_specialization) {
|
||||||
|
test_detail::partial_opt_out_wrapper<int> obj;
|
||||||
|
EXPECT_EQ(fmt::format("{}", obj), "PartialOptOut(size=3)");
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user