From ec6651087d05e8a3e4e886e2673a492ee3a0db88 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 2 Jun 2019 16:04:17 -0700 Subject: [PATCH] Remove old is_constructible workarounds and replace typedefs with using --- include/fmt/core.h | 114 +++++++++++++++++-------------------------- include/fmt/printf.h | 9 ++-- test/core-test.cc | 18 +++---- 3 files changed, 56 insertions(+), 85 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 3298b05f..524c47d4 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -204,6 +204,10 @@ FMT_BEGIN_NAMESPACE template using enable_if_t = typename std::enable_if::type; +// An enable_if helper to be used in template parameters which results in much +// shorter symbols: https://godbolt.org/z/sWw4vP. +#define FMT_ENABLE_IF(...) enable_if_t<__VA_ARGS__, int> = 0 + namespace internal { #if defined(FMT_USE_STRING_VIEW) @@ -215,10 +219,6 @@ using std_string_view = std::experimental::basic_string_view; template struct std_string_view {}; #endif -// An enable_if helper to be used in template parameters which results in much -// shorter symbols: https://godbolt.org/z/sWw4vP. -#define FMT_ENABLE_IF(...) enable_if_t<__VA_ARGS__, int> = 0 - #if (__cplusplus >= 201703L || \ (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)) && \ __cpp_lib_is_invocable >= 201703L @@ -266,8 +266,8 @@ template class buffer { virtual void grow(std::size_t capacity) = 0; public: - typedef T value_type; - typedef const T& const_reference; + using value_type = T; + using const_reference = const T&; virtual ~buffer() {} @@ -334,7 +334,7 @@ class container_buffer : public buffer { // Extracts a reference to the container from back_insert_iterator. template inline Container& get_container(std::back_insert_iterator it) { - typedef std::back_insert_iterator bi_iterator; + using bi_iterator = std::back_insert_iterator; struct accessor : bi_iterator { accessor(bi_iterator iter) : bi_iterator(iter) {} using bi_iterator::container; @@ -349,23 +349,6 @@ struct error_handler { // This function is intentionally not constexpr to give a compile-time error. FMT_API FMT_NORETURN void on_error(const char* message); }; - -// GCC 4.6.x cannot expand `T...`. -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 407 -typedef char yes[1]; -typedef char no[2]; - -template struct is_constructible { - template - static yes& test(int (*)[sizeof(new U(std::declval()))]); - template static no& test(...); - enum { value = sizeof(test(nullptr)) == sizeof(yes) }; -}; -#else -template -struct is_constructible : std::is_constructible {}; -#endif -struct dummy_formatter_arg {}; // Workaround broken is_constructible in MSVC. } // namespace internal /** @@ -381,8 +364,8 @@ template class basic_string_view { size_t size_; public: - typedef Char char_type; - typedef const Char* iterator; + using char_type = Char; + using iterator = const Char*; FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(nullptr), size_(0) {} @@ -455,8 +438,8 @@ template class basic_string_view { } }; -typedef basic_string_view string_view; -typedef basic_string_view wstring_view; +using string_view = basic_string_view; +using wstring_view = basic_string_view; /** \rst @@ -525,8 +508,8 @@ class basic_parse_context : private ErrorHandler { int next_arg_id_; public: - typedef Char char_type; - typedef typename basic_string_view::iterator iterator; + using char_type = Char; + using iterator = typename basic_string_view::iterator; explicit FMT_CONSTEXPR basic_parse_context(basic_string_view format_str, ErrorHandler eh = ErrorHandler()) @@ -567,11 +550,11 @@ class basic_parse_context : private ErrorHandler { FMT_CONSTEXPR ErrorHandler error_handler() const { return *this; } }; -typedef basic_parse_context format_parse_context; -typedef basic_parse_context wformat_parse_context; +using format_parse_context = basic_parse_context; +using wformat_parse_context = basic_parse_context; -FMT_DEPRECATED typedef basic_parse_context parse_context; -FMT_DEPRECATED typedef basic_parse_context wparse_context; +using parse_context FMT_DEPRECATED = basic_parse_context; +using wparse_context FMT_DEPRECATED = basic_parse_context; template class basic_format_arg; template class basic_format_args; @@ -579,7 +562,7 @@ template class basic_format_args; // A formatter for objects of type T. template struct formatter { - explicit formatter(internal::dummy_formatter_arg); + formatter() = delete; }; template @@ -669,18 +652,14 @@ template struct custom_value { Context& ctx); }; -template struct is_formattable { - enum { - value = - !is_constructible::type, - internal::dummy_formatter_arg>::value - }; -}; +template +using is_formattable = + std::is_constructible>; // A formatting argument value. template class value { public: - typedef typename Context::char_type char_type; + using char_type = typename Context::char_type; union { int int_value; @@ -727,7 +706,7 @@ template class value { custom.format = &format_custom_arg< T, typename std::conditional< is_formattable::value, - typename Context::template formatter_type::type, + typename Context::template formatter_type, internal::fallback_formatter>::type>; } @@ -779,12 +758,12 @@ FMT_MAKE_VALUE_SAME(uint_type, unsigned) // To minimize the number of types we need to deal with, long is translated // either to int or to long long depending on its size. -typedef std::conditional::type - long_type; +using long_type = + std::conditional::type; FMT_MAKE_VALUE((sizeof(long) == sizeof(int) ? int_type : long_long_type), long, long_type) -typedef std::conditional::type ulong_type; +using ulong_type = std::conditional::type; FMT_MAKE_VALUE((sizeof(unsigned long) == sizeof(unsigned) ? uint_type : ulong_long_type), unsigned long, ulong_type) @@ -849,9 +828,10 @@ inline init make_value(const T& val) { return static_cast(val); } -template , T>::value && - !internal::is_string::value)> +template < + typename C, typename T, typename Char = typename C::char_type, + FMT_ENABLE_IF(std::is_constructible, T>::value && + !internal::is_string::value)> inline init, string_type> make_value(const T& val) { return basic_string_view(val); } @@ -864,7 +844,7 @@ template < FMT_ENABLE_IF(!convert_to_int::value && !std::is_same::value && !std::is_convertible>::value && - !is_constructible, U>::value && + !std::is_constructible, U>::value && !internal::is_string::value)> inline init make_value(const T& val) { return val; @@ -913,7 +893,7 @@ template class basic_format_arg { friend class basic_format_args; friend class internal::arg_map; - typedef typename Context::char_type char_type; + using char_type = typename Context::char_type; public: class handle { @@ -952,7 +932,7 @@ struct monostate {}; template FMT_CONSTEXPR internal::invoke_result_t visit_format_arg( Visitor&& vis, const basic_format_arg& arg) { - typedef typename Context::char_type char_type; + using char_type = typename Context::char_type; switch (arg.type_) { case internal::none_type: break; @@ -1001,7 +981,7 @@ template class arg_map { arg_map(const arg_map&) = delete; void operator=(const arg_map&) = delete; - typedef typename Context::char_type char_type; + using char_type = typename Context::char_type; struct entry { basic_string_view name; @@ -1084,7 +1064,7 @@ inline basic_format_arg make_arg(const T& value) { template class basic_format_context { public: /** The character type for the output. */ - typedef Char char_type; + using char_type = Char; private: OutputIt out_; @@ -1096,13 +1076,9 @@ template class basic_format_context { void operator=(const basic_format_context&) = delete; public: - typedef OutputIt iterator; - typedef basic_format_arg format_arg; - - // using formatter_type = formatter; - template struct formatter_type { - typedef formatter type; - }; + using iterator = OutputIt; + using format_arg = basic_format_arg ; + template using formatter_type = formatter; /** Constructs a ``basic_format_context`` object. References to the arguments are @@ -1137,8 +1113,8 @@ template struct buffer_context { std::back_insert_iterator>, Char> type; }; -typedef buffer_context::type format_context; -typedef buffer_context::type wformat_context; +using format_context = buffer_context::type; +using wformat_context = buffer_context::type; /** \rst @@ -1154,8 +1130,8 @@ template class format_arg_store { // Packed is a macro on MinGW so use IS_PACKED instead. static const bool IS_PACKED = NUM_ARGS < internal::max_packed_args; - typedef typename std::conditional, - basic_format_arg>::type value_type; + using value_type = typename std::conditional, + basic_format_arg>::type; // If the arguments are not packed, add one more element to mark the end. static const size_t DATA_SIZE = @@ -1213,8 +1189,8 @@ inline format_arg_store make_format_args( /** Formatting arguments. */ template class basic_format_args { public: - typedef unsigned size_type; - typedef basic_format_arg format_arg; + using size_type = unsigned; + using format_arg = basic_format_arg; private: // To reduce compiled code size per formatting function call, types of first diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 3dbf1fc0..29f36323 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -329,12 +329,9 @@ template struct printf_formatter { template class basic_printf_context { public: /** The character type for the output. */ - typedef Char char_type; - typedef basic_format_arg format_arg; - - template struct formatter_type { - typedef printf_formatter type; - }; + using char_type = Char; + using format_arg = basic_format_arg; + template using formatter_type = printf_formatter; private: typedef basic_format_specs format_specs; diff --git a/test/core-test.cc b/test/core-test.cc index 5f210b8d..ff868e14 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -209,17 +209,15 @@ struct custom_context { typedef char char_type; template struct formatter_type { - struct type { - template - auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } + template + auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } - const char* format(const T&, custom_context& ctx) { - ctx.called = true; - return nullptr; - } - }; + const char* format(const T&, custom_context& ctx) { + ctx.called = true; + return nullptr; + } }; bool called;