use is_constant_evaluated() to initialize class value` with string

This commit is contained in:
Alexey Ochapov 2020-12-15 04:48:34 +03:00
parent 6742642dcd
commit de42f13712
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8
2 changed files with 14 additions and 9 deletions

View File

@ -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 <typename T> constexpr T const_check(T value) { return value; }
@ -1082,7 +1090,12 @@ template <typename Context> 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<char_type> val) {
string.data = val.data();
string.size = val.size();

View File

@ -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<Dest*>(&source)` that doesn't have
// undefined behavior (e.g. due to type aliasing).
// Example: uint64_t d = bit_cast<uint64_t>(2.718);