Skip to content

Commit

Permalink
Fix formatting of objects convertible to std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 17, 2018
1 parent ce4a65f commit 18ac987
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 7 additions & 6 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,8 @@ FMT_MAKE_VALUE(cstring_type, signed char*, const signed char*)
FMT_MAKE_VALUE(cstring_type, const signed char*, const signed char*)
FMT_MAKE_VALUE(cstring_type, unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(cstring_type, const unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(string_type, basic_string_view<Char>,
basic_string_view<Char>)
FMT_MAKE_VALUE(string_type, const std::basic_string<Char>&,
FMT_MAKE_VALUE(string_type, basic_string_view<Char>, basic_string_view<Char>)
FMT_MAKE_VALUE(string_type, const std::basic_string<typename C::char_type>&,
basic_string_view<Char>)
FMT_MAKE_VALUE(pointer_type, void*, const void*)
FMT_MAKE_VALUE(pointer_type, const void*, const void*)
Expand All @@ -593,9 +592,11 @@ inline typename std::enable_if<
typed_value<C, int_type>>::type
make_value(const T &val) { return static_cast<int>(val); }

template <typename C, typename T>
inline typename std::enable_if<!convert_to_int<
T, typename C::char_type>::value, typed_value<C, custom_type>>::type
template <typename C, typename T, typename Char = typename C::char_type>
inline typename std::enable_if<
!convert_to_int<T, Char>::value &&
!std::is_convertible<T, std::basic_string<Char>>::value,
typed_value<C, custom_type>>::type
make_value(const T &val) { return val; }

template <typename C, typename T>
Expand Down
10 changes: 10 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,16 @@ TEST(FormatterTest, FormatStringView) {
EXPECT_EQ("test", format("{0}", string_view("test")));
}

struct ConvertibleToString {
std::string s;
ConvertibleToString() : s("foo") {}
operator const std::string &() const { return s; }
};

TEST(FormatterTest, FormatConvertibleToString) {
EXPECT_EQ("foo", format("{}", ConvertibleToString()));
}

namespace fmt {
template <>
struct formatter<Date> {
Expand Down

0 comments on commit 18ac987

Please sign in to comment.