From cdc5ef6710829bc22f22f3f7c196e5d24c3a2725 Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Wed, 30 Dec 2020 17:23:20 +0300 Subject: [PATCH] Remove fallback to inline specifier from FMT_CONSTEXPR(20) macro (#2075) --- include/fmt/chrono.h | 72 ++++++++++++++++++++++++++++++++------------ include/fmt/color.h | 7 +++-- include/fmt/core.h | 21 +++++++------ include/fmt/format.h | 28 +++++++++-------- 4 files changed, 85 insertions(+), 43 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 0e06e18c..d68e8148 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -456,27 +456,61 @@ namespace detail { template FMT_CONSTEXPR const char* get_units() { return nullptr; } -template <> FMT_CONSTEXPR const char* get_units() { return "as"; } -template <> FMT_CONSTEXPR const char* get_units() { return "fs"; } -template <> FMT_CONSTEXPR const char* get_units() { return "ps"; } -template <> FMT_CONSTEXPR const char* get_units() { return "ns"; } -template <> FMT_CONSTEXPR const char* get_units() { return "µs"; } -template <> FMT_CONSTEXPR const char* get_units() { return "ms"; } -template <> FMT_CONSTEXPR const char* get_units() { return "cs"; } -template <> FMT_CONSTEXPR const char* get_units() { return "ds"; } -template <> FMT_CONSTEXPR const char* get_units>() { return "s"; } -template <> FMT_CONSTEXPR const char* get_units() { return "das"; } -template <> FMT_CONSTEXPR const char* get_units() { return "hs"; } -template <> FMT_CONSTEXPR const char* get_units() { return "ks"; } -template <> FMT_CONSTEXPR const char* get_units() { return "Ms"; } -template <> FMT_CONSTEXPR const char* get_units() { return "Gs"; } -template <> FMT_CONSTEXPR const char* get_units() { return "Ts"; } -template <> FMT_CONSTEXPR const char* get_units() { return "Ps"; } -template <> FMT_CONSTEXPR const char* get_units() { return "Es"; } -template <> FMT_CONSTEXPR const char* get_units>() { +template <> FMT_CONSTEXPR inline const char* get_units() { + return "as"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "fs"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "ps"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "ns"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "µs"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "ms"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "cs"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "ds"; +} +template <> FMT_CONSTEXPR inline const char* get_units>() { + return "s"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "das"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "hs"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "ks"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "Ms"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "Gs"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "Ts"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "Ps"; +} +template <> FMT_CONSTEXPR inline const char* get_units() { + return "Es"; +} +template <> FMT_CONSTEXPR inline const char* get_units>() { return "m"; } -template <> FMT_CONSTEXPR const char* get_units>() { +template <> FMT_CONSTEXPR inline const char* get_units>() { return "h"; } diff --git a/include/fmt/color.h b/include/fmt/color.h index 42008f51..24ccedac 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -353,16 +353,17 @@ class text_style { }; /** Creates a text style from the foreground (text) color. */ -FMT_CONSTEXPR text_style fg(detail::color_type foreground) FMT_NOEXCEPT { +FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) FMT_NOEXCEPT { return text_style(true, foreground); } /** Creates a text style from the background color. */ -FMT_CONSTEXPR text_style bg(detail::color_type background) FMT_NOEXCEPT { +FMT_CONSTEXPR inline text_style bg(detail::color_type background) FMT_NOEXCEPT { return text_style(false, background); } -FMT_CONSTEXPR text_style operator|(emphasis lhs, emphasis rhs) FMT_NOEXCEPT { +FMT_CONSTEXPR inline text_style operator|(emphasis lhs, + emphasis rhs) FMT_NOEXCEPT { return text_style(lhs) | rhs; } diff --git a/include/fmt/core.h b/include/fmt/core.h index ad7dcd83..ec729116 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -92,7 +92,7 @@ # define FMT_CONSTEXPR constexpr # define FMT_CONSTEXPR_DECL constexpr #else -# define FMT_CONSTEXPR inline +# define FMT_CONSTEXPR # define FMT_CONSTEXPR_DECL #endif @@ -943,9 +943,9 @@ struct arg_data { T args_[NUM_ARGS != 0 ? NUM_ARGS : +1]; template - FMT_CONSTEXPR arg_data(const U&... init) : args_{init...} {} - FMT_CONSTEXPR const T* args() const { return args_; } - FMT_CONSTEXPR std::nullptr_t named_args() { return nullptr; } + FMT_CONSTEXPR FMT_INLINE arg_data(const U&... init) : args_{init...} {} + FMT_CONSTEXPR FMT_INLINE const T* args() const { return args_; } + FMT_CONSTEXPR FMT_INLINE std::nullptr_t named_args() { return nullptr; } }; template @@ -966,7 +966,8 @@ void init_named_args(named_arg_info* named_args, int arg_count, } template -FMT_CONSTEXPR void init_named_args(std::nullptr_t, int, int, const Args&...) {} +FMT_CONSTEXPR FMT_INLINE void init_named_args(std::nullptr_t, int, int, + const Args&...) {} template struct is_named_arg : std::false_type {}; @@ -1087,11 +1088,11 @@ template class value { FMT_INLINE value(long double val) : long_double_value(val) {} constexpr FMT_INLINE value(bool val) : bool_value(val) {} constexpr FMT_INLINE value(char_type val) : char_value(val) {} - FMT_CONSTEXPR value(const char_type* val) { + FMT_CONSTEXPR FMT_INLINE value(const char_type* val) { string.data = val; if (is_constant_evaluated()) string.size = {}; } - FMT_CONSTEXPR value(basic_string_view val) { + FMT_CONSTEXPR FMT_INLINE value(basic_string_view val) { string.data = val.data(); string.size = val.size(); } @@ -1682,7 +1683,8 @@ template class basic_format_args { \endrst */ template - constexpr basic_format_args(const format_arg_store& store) + constexpr FMT_INLINE basic_format_args( + const format_arg_store& store) : basic_format_args(store.desc, store.data_.args()) {} /** @@ -1691,7 +1693,8 @@ template class basic_format_args { `~fmt::dynamic_format_arg_store`. \endrst */ - constexpr basic_format_args(const dynamic_format_arg_store& store) + constexpr FMT_INLINE basic_format_args( + const dynamic_format_arg_store& store) : basic_format_args(store.get_types(), store.data()) {} /** diff --git a/include/fmt/format.h b/include/fmt/format.h index 3d18b9e1..9ec77ab7 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -287,7 +287,7 @@ namespace detail { (__cplusplus >= 201709L && FMT_GCC_VERSION >= 1002) # define FMT_CONSTEXPR20 constexpr #else -# define FMT_CONSTEXPR20 inline +# define FMT_CONSTEXPR20 #endif // An equivalent of `*reinterpret_cast(&source)` that doesn't have @@ -545,7 +545,7 @@ inline size_t count_code_points(basic_string_view s) { } // Counts the number of code points in a UTF-8 string. -FMT_CONSTEXPR size_t count_code_points(basic_string_view s) { +FMT_CONSTEXPR inline size_t count_code_points(basic_string_view s) { const char* data = s.data(); size_t num_code_points = 0; for (size_t i = 0, size = s.size(); i != size; ++i) { @@ -984,7 +984,7 @@ template FMT_CONSTEXPR int count_digits_fallback(T n) { #ifdef FMT_BUILTIN_CLZLL // Returns the number of decimal digits in n. Leading zeros are not counted // except for n == 0 in which case count_digits returns 1. -FMT_CONSTEXPR20 int count_digits(uint64_t n) { +FMT_CONSTEXPR20 inline int count_digits(uint64_t n) { if (is_constant_evaluated()) { return count_digits_fallback(n); } @@ -994,11 +994,13 @@ FMT_CONSTEXPR20 int count_digits(uint64_t n) { } #else // Fallback version of count_digits used when __builtin_clz is not available. -FMT_CONSTEXPR int count_digits(uint64_t n) { return count_digits_fallback(n); } +FMT_CONSTEXPR inline int count_digits(uint64_t n) { + return count_digits_fallback(n); +} #endif #if FMT_USE_INT128 -FMT_CONSTEXPR int count_digits(uint128_t n) { +FMT_CONSTEXPR inline int count_digits(uint128_t n) { int count = 1; for (;;) { // Integer division is slow so do it for a group of four digits instead @@ -1035,7 +1037,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n); #ifdef FMT_BUILTIN_CLZ // Optional version of count_digits for better performance on 32-bit platforms. -FMT_CONSTEXPR20 int count_digits(uint32_t n) { +FMT_CONSTEXPR20 inline int count_digits(uint32_t n) { if (is_constant_evaluated()) { return count_digits_fallback(n); } @@ -2320,7 +2322,7 @@ class arg_formatter_base { } template ::value)> - FMT_CONSTEXPR iterator operator()(T value) { + FMT_CONSTEXPR FMT_INLINE iterator operator()(T value) { if (specs_) write_int(value, *specs_); else @@ -4039,11 +4041,11 @@ FMT_CONSTEXPR detail::udl_formatter operator""_format() { std::string message = "The answer is {}"_format(42); \endrst */ -FMT_CONSTEXPR detail::udl_formatter operator"" _format(const char* s, - size_t n) { +FMT_CONSTEXPR inline detail::udl_formatter operator"" _format( + const char* s, size_t n) { return {{s, n}}; } -FMT_CONSTEXPR detail::udl_formatter operator"" _format( +FMT_CONSTEXPR inline detail::udl_formatter operator"" _format( const wchar_t* s, size_t n) { return {{s, n}}; } @@ -4059,10 +4061,12 @@ FMT_CONSTEXPR detail::udl_formatter operator"" _format( fmt::print("Elapsed time: {s:.2f} seconds", "s"_a=1.23); \endrst */ -FMT_CONSTEXPR detail::udl_arg operator"" _a(const char* s, size_t) { +FMT_CONSTEXPR inline detail::udl_arg operator"" _a(const char* s, + size_t) { return {s}; } -FMT_CONSTEXPR detail::udl_arg operator"" _a(const wchar_t* s, size_t) { +FMT_CONSTEXPR inline detail::udl_arg operator"" _a(const wchar_t* s, + size_t) { return {s}; } } // namespace literals