From 31e743d06e739405eab934d30ba73461244ae13e Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 4 Feb 2022 11:00:00 -0800 Subject: [PATCH] Don't use ostream for types convertible to string_view --- include/fmt/core.h | 2 +- include/fmt/ostream.h | 2 +- test/ostream-test.cc | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 68b86852..f64f1a69 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -9,7 +9,7 @@ #define FMT_CORE_H_ #include // std::FILE -#include +#include // std::strlen #include #include #include diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 5a2a5456..7acc3299 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -45,7 +45,7 @@ struct is_streamable< enable_if_t< std::is_arithmetic::value || std::is_array::value || std::is_pointer::value || std::is_same::value || - std::is_same>::value || + std::is_convertible>::value || std::is_same>::value || (std::is_convertible::value && !std::is_enum::value)>> : std::false_type {}; diff --git a/test/ostream-test.cc b/test/ostream-test.cc index f81039e5..bae7ad18 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -262,6 +262,21 @@ TEST(ostream_test, format_convertible_to_bool) { EXPECT_EQ(fmt::format("{}", streamable_and_convertible_to_bool()), "true"); } +struct streamable_and_convertible_to_string_view { + operator fmt::string_view() const { return "foo"; } +}; + +std::ostream& operator<<(std::ostream& os, + streamable_and_convertible_to_string_view) { + return os << "bar"; +} + +TEST(ostream_test, format_convertible_to_string_vew) { + // operator<< is intentionally not used because of potential ODR violations. + EXPECT_EQ(fmt::format("{}", streamable_and_convertible_to_string_view()), + "foo"); +} + struct copyfmt_test {}; std::ostream& operator<<(std::ostream& os, copyfmt_test) {