From 44862b475d4a2ac721f2d2119f595ecd72d57e21 Mon Sep 17 00:00:00 2001 From: timsong-cpp Date: Thu, 23 Jan 2025 08:25:20 -0600 Subject: [PATCH] Fix #4303: avoid instantiating formatter --- include/fmt/base.h | 2 +- test/std-test.cc | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 1a6ccf24bbf9..9eeeb2ce7c53 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -1121,7 +1121,7 @@ using use_formatter = bool_constant<(std::is_class::value || std::is_enum::value || std::is_union::value || std::is_array::value) && !has_to_string_view::value && !is_named_arg::value && - !use_format_as::value && !use_format_as_member::value>; + !use_format_as::value && !use_format_as_member::value>; template > auto has_formatter_impl(T* p, buffered_context* ctx = nullptr) diff --git a/test/std-test.cc b/test/std-test.cc index 2c57b3f6e394..760daf8f1ace 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -13,13 +13,17 @@ #include #include "fmt/os.h" // fmt::system_category -#include "fmt/ranges.h" #include "gtest-extra.h" // StartsWith #ifdef __cpp_lib_filesystem TEST(std_test, path) { using std::filesystem::path; EXPECT_EQ(fmt::format("{}", path("/usr/bin")), "/usr/bin"); + + // see #4303 + const path p = "/usr/bin"; + EXPECT_EQ(fmt::format("{}", p), "/usr/bin"); + EXPECT_EQ(fmt::format("{:?}", path("/usr/bin")), "\"/usr/bin\""); EXPECT_EQ(fmt::format("{:8}", path("foo")), "foo "); @@ -44,6 +48,9 @@ TEST(std_test, path) { # endif } +// Intentionally delayed include to test #4303 +#include "fmt/ranges.h" + // Test ambiguity problem described in #2954. TEST(ranges_std_test, format_vector_path) { auto p = std::filesystem::path("foo/bar.txt");