Don't use ostream for types convertible to string_view

This commit is contained in:
Victor Zverovich 2022-02-04 11:00:00 -08:00
parent 35c0286cd8
commit 31e743d06e
3 changed files with 17 additions and 2 deletions

View File

@ -9,7 +9,7 @@
#define FMT_CORE_H_
#include <cstdio> // std::FILE
#include <cstring>
#include <cstring> // std::strlen
#include <iterator>
#include <limits>
#include <string>

View File

@ -45,7 +45,7 @@ struct is_streamable<
enable_if_t<
std::is_arithmetic<T>::value || std::is_array<T>::value ||
std::is_pointer<T>::value || std::is_same<T, char8_type>::value ||
std::is_same<T, std::basic_string<Char>>::value ||
std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
std::is_same<T, std_string_view<Char>>::value ||
(std::is_convertible<T, int>::value && !std::is_enum<T>::value)>>
: std::false_type {};

View File

@ -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) {