From 140edee7806b6b401684cabea1103d15b0b2866d Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Sun, 26 Feb 2023 11:23:50 +0500 Subject: [PATCH] std::filesystem::path is not format as a range Signed-off-by: Vladislav Shchapov --- include/fmt/ranges.h | 14 +++++++++++++- test/ranges-test.cc | 9 ++++++--- test/std-test.cc | 6 ++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 00bea734..ce7ed1ba 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -59,6 +59,17 @@ template class is_std_string_like { !std::is_void(nullptr))>::value; }; +// Returns true if T has a std::filesystem::path-like interface. +template class is_std_filesystem_path_like { + template + static auto check(U* p) -> decltype((void)U::preferred_separator, int()); + template static void check(...); + + public: + static constexpr const bool value = + !std::is_void(nullptr))>::value; +}; + template struct is_std_string_like> : std::true_type {}; @@ -383,7 +394,8 @@ template struct is_range { static constexpr const bool value = detail::is_range_::value && !detail::is_std_string_like::value && !std::is_convertible>::value && - !std::is_convertible>::value; + !std::is_convertible>::value && + !detail::is_std_filesystem_path_like::value; }; namespace detail { diff --git a/test/ranges-test.cc b/test/ranges-test.cc index e4a2d0e3..6152d038 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -179,15 +179,18 @@ TEST(ranges_test, format_to) { EXPECT_STREQ(buf, "[1, 2, 3]"); } -struct path_like { +template struct path_like { const path_like* begin() const; const path_like* end() const; - operator std::string() const; + operator std::basic_string() const; + + static constexpr const Char preferred_separator = Char(); }; TEST(ranges_test, path_like) { - EXPECT_FALSE((fmt::is_range::value)); + EXPECT_FALSE((fmt::is_range, char>::value)); + EXPECT_FALSE((fmt::is_range, char>::value)); } // A range that provides non-const only begin()/end() to test fmt::join diff --git a/test/std-test.cc b/test/std-test.cc index 39aac5a2..29f9df4e 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -46,6 +46,12 @@ TEST(ranges_std_test, format_vector_path) { #endif } +TEST(ranges_std_test, path) { +#ifdef __cpp_lib_filesystem + EXPECT_FALSE((fmt::is_range::value)); +#endif +} + TEST(std_test, thread_id) { EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty()); }