From e2e4c5271d92f6fddba6a832affdadbab59a5013 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 8 Dec 2023 14:04:43 +0800 Subject: [PATCH] allow format_as() to format reference before this change, format_as() is unable to format a type which has `auto format_as() -> const another_type&`, and `another_type` is formattable. because `format_as_result` maps the result type as it is, and the compiler refuses to compile `static_cast(nullptr)`, where T is a reference type. but it would be handy if we could use `format_as()` to format types which, for instance, owns / inherit from a formattable type, and delegate the formatter to these variables instead without creating a copy of them. in this change: * instruct `format_as_result` to map the result type to the decayed type, so that `type` can be the decayed type of result type, and this also enables `type` to be formattable, as long as the decayed type is formattable. * corresponding test is added to format-test.cc Signed-off-by: Kefu Chai --- include/fmt/core.h | 2 +- test/format-test.cc | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index b19abf37095d..3bb281b26b9a 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1332,7 +1332,7 @@ using ulong_type = conditional_t; template struct format_as_result { template ::value || std::is_class::value)> - static auto map(U*) -> decltype(format_as(std::declval())); + static auto map(U*) -> remove_cvref_t()))>; static auto map(...) -> void; using type = decltype(map(static_cast(nullptr))); diff --git a/test/format-test.cc b/test/format-test.cc index 0a9924bf9bf9..a708dd008baa 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2173,6 +2173,13 @@ auto format_as(scoped_enum_as_string) -> std::string { return "foo"; } struct struct_as_int {}; auto format_as(struct_as_int) -> int { return 42; } + +struct struct_as_const_reference { + const std::string name = "foo"; +}; +auto format_as(const struct_as_const_reference& s) -> const std::string& { + return s.name; +} } // namespace test TEST(format_test, format_as) { @@ -2180,6 +2187,7 @@ TEST(format_test, format_as) { EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string_view()), "foo"); EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string()), "foo"); EXPECT_EQ(fmt::format("{}", test::struct_as_int()), "42"); + EXPECT_EQ(fmt::format("{}", test::struct_as_const_reference()), "foo"); } TEST(format_test, format_as_to_string) {