From ea9b80444fcba04713a449dd18f03fd84cb54fc2 Mon Sep 17 00:00:00 2001 From: John Eivind Helset Date: Wed, 22 Jun 2022 06:29:18 +0200 Subject: [PATCH] Cleanup `write_variant_alternative`. --- include/fmt/std.h | 50 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index e2041b8df6ed9..a8de3a3bef660 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -113,48 +113,14 @@ template class is_variant_formattable_ { decltype(check(variant_index_sequence{}))::value; }; -template -auto write_variant_alternative(OutputIt out, basic_string_view str) - -> OutputIt { - return write_escaped_string(out, str); -} - -// Returns true if T has a std::string-like interface, like std::string_view. -template class is_std_string_likevv { - template - static auto check(U* p) - -> decltype((void)p->find('a'), p->length(), (void)p->data(), int()); - template static void check(...); - - public: - static constexpr const bool value = - is_string::value || - std::is_convertible>::value || - !std::is_void(nullptr))>::value; -}; - -template -struct is_std_string_likevv> : std::true_type {}; - -template >::value)> -inline auto write_variant_alternative(OutputIt out, const T& str) -> OutputIt { - auto sv = std_string_view(str); - return write_variant_alternative(out, basic_string_view(sv)); -} - -template ::value)> -OutputIt write_variant_alternative(OutputIt out, const Arg v) { - return write_escaped_char(out, v); -} - -template ::type>::value && - !std::is_same::value)> -OutputIt write_variant_alternative(OutputIt out, const Arg& v) { - return write(out, v); +template +auto write_variant_alternative(OutputIt out, const T &v) -> OutputIt { + if constexpr(is_string::value) + return write_escaped_string(out, detail::to_string_view(v)); + else if constexpr(std::is_same_v) + return write_escaped_char(out, v); + else + return write(out, v); } } // namespace detail