From de42f13712cbd6476a252a0cb9e25feb8122ba4d Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Tue, 15 Dec 2020 04:48:34 +0300 Subject: [PATCH] use `is_constant_evaluated()`` to initialize class `value` with string --- include/fmt/core.h | 15 ++++++++++++++- include/fmt/format.h | 8 -------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index e7204be5..e2db21af 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -283,6 +283,14 @@ struct monostate {}; namespace detail { +constexpr bool is_constant_evaluated() FMT_DETECTED_NOEXCEPT { +#ifdef __cpp_lib_is_constant_evaluated + return std::is_constant_evaluated(); +#else + return false; +#endif +} + // A helper function to suppress "conditional expression is constant" warnings. template constexpr T const_check(T value) { return value; } @@ -1082,7 +1090,12 @@ 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) : string() { string.data = val; } + FMT_CONSTEXPR value(const char_type* val) { + string.data = val; + if (is_constant_evaluated()) { + string.size = {}; + } + } FMT_CONSTEXPR value(basic_string_view val) { string.data = val.data(); string.size = val.size(); diff --git a/include/fmt/format.h b/include/fmt/format.h index 09f2d782..774d5a4b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -288,14 +288,6 @@ namespace detail { # define FMT_CONSTEXPR20 inline #endif -constexpr bool is_constant_evaluated() FMT_DETECTED_NOEXCEPT { -#ifdef __cpp_lib_is_constant_evaluated - return std::is_constant_evaluated(); -#else - return false; -#endif -} - // An equivalent of `*reinterpret_cast(&source)` that doesn't have // undefined behavior (e.g. due to type aliasing). // Example: uint64_t d = bit_cast(2.718);