Simplify compile-time checks

This commit is contained in:
Victor Zverovich 2024-08-31 14:49:59 -07:00
parent 516a2e2049
commit 8c4b17fe64

View File

@ -3014,20 +3014,14 @@ template <typename Char, typename... Args> class basic_format_string {
using checker = detail::format_string_checker<Char, remove_cvref_t<Args>...>; using checker = detail::format_string_checker<Char, remove_cvref_t<Args>...>;
template <typename S> FMT_CONSTEXPR FMT_ALWAYS_INLINE void check() const {
FMT_CONSTEXPR FMT_ALWAYS_INLINE void check(const S& s) { using namespace detail;
static_assert( static_assert(
detail::count< count<(std::is_base_of<view, remove_reference_t<Args>>::value &&
(std::is_base_of<detail::view, remove_reference_t<Args>>::value &&
std::is_reference<Args>::value)...>() == 0, std::is_reference<Args>::value)...>() == 0,
"passing views as lvalues is disallowed"); "passing views as lvalues is disallowed");
detail::ignore_unused(s); if (count_named_args<Args...>() == count_statically_named_args<Args...>())
#if FMT_USE_CONSTEVAL parse_format_string(str_, checker(str_));
if constexpr (detail::count_named_args<Args...>() ==
detail::count_statically_named_args<Args...>()) {
detail::parse_format_string(str_, checker(str_));
}
#endif
} }
public: public:
@ -3036,7 +3030,7 @@ template <typename Char, typename... Args> class basic_format_string {
FMT_ENABLE_IF( FMT_ENABLE_IF(
std::is_convertible<const S&, basic_string_view<Char>>::value)> std::is_convertible<const S&, basic_string_view<Char>>::value)>
FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_format_string(const S& s) : str_(s) { FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_format_string(const S& s) : str_(s) {
check(s); if (FMT_USE_CONSTEVAL) check();
#ifdef FMT_ENFORCE_COMPILE_STRING #ifdef FMT_ENFORCE_COMPILE_STRING
static_assert( static_assert(
FMT_USE_CONSTEVAL && sizeof(S) != 0, FMT_USE_CONSTEVAL && sizeof(S) != 0,
@ -3048,8 +3042,8 @@ template <typename Char, typename... Args> class basic_format_string {
FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&& FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&&
std::is_constructible<basic_string_view<Char>, std::is_constructible<basic_string_view<Char>,
const S&>::value)> const S&>::value)>
FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_format_string(const S& s) : str_(s) { FMT_ALWAYS_INLINE basic_format_string(const S& s) : str_(s) {
check(s); check();
if (FMT_USE_CONSTEVAL) return; if (FMT_USE_CONSTEVAL) return;
FMT_CONSTEXPR auto v = basic_string_view<Char>(S()); FMT_CONSTEXPR auto v = basic_string_view<Char>(S());
FMT_CONSTEXPR int ignore = (detail::parse_format_string(v, checker(v)), 0); FMT_CONSTEXPR int ignore = (detail::parse_format_string(v, checker(v)), 0);