Skip to content

Commit

Permalink
Fix handling of formattable types with to_string_view (#2181)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 19, 2021
1 parent 6ae402f commit 14a2a64
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ FMT_END_NAMESPACE
// Some compilers masquerade as both MSVC and GCC-likes or otherwise support
// __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the
// MSVC intrinsics if the clz and clzll builtins are not available.
#if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && \
!defined(FMT_BUILTIN_CTZLL)
#if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(FMT_BUILTIN_CTZLL)
FMT_BEGIN_NAMESPACE
namespace detail {
// Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning.
Expand Down Expand Up @@ -2103,6 +2102,12 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, basic_string_view<Char> value) {
return base_iterator(out, it);
}

template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(is_string<T>::value)>
constexpr OutputIt write(OutputIt out, const T& value) {
return write<Char>(out, to_string_view(value));
}

template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(is_integral<T>::value &&
!std::is_same<T, bool>::value &&
Expand Down
21 changes: 21 additions & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,27 @@ TEST(CompileTest, UnknownFormatFallback) {
}

TEST(CompileTest, Empty) { EXPECT_EQ("", fmt::format(FMT_COMPILE(""))); }

struct to_stringable {
friend fmt::string_view to_string_view(to_stringable) { return {}; }
};

FMT_BEGIN_NAMESPACE
template <> struct formatter<to_stringable> {
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
auto format(const to_stringable&, FormatContext& ctx) -> decltype(ctx.out()) {
return ctx.out();
}
};
FMT_END_NAMESPACE

TEST(CompileTest, ToStringAndFormatter) {
fmt::format(FMT_COMPILE("{}"), to_stringable());
}
#endif

#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
Expand Down

0 comments on commit 14a2a64

Please sign in to comment.