From 21425c05e0a91a2b16503958bbf0201c108df967 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 5 Dec 2022 12:59:10 +0100 Subject: [PATCH] Add a centralized dependent_false type The main use case is to static_assert on it at the end of a if constexpr / else if constexpr chain. See P2593 for a discussion about why this is pretty much the only "allowed" way of doing so, short of running into IFNDR. I'm actually adding two versions: one for TTP and one for NTTP, as Qt code uses both. Apply it in QFlatMap. Change-Id: Iaff97e350784683d0c3994020b1352d5188931d6 Reviewed-by: Thiago Macieira --- src/corelib/global/qttypetraits.h | 7 +++++++ src/corelib/tools/qflatmap_p.h | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/corelib/global/qttypetraits.h b/src/corelib/global/qttypetraits.h index e8939e7d5f..7e991ab8ca 100644 --- a/src/corelib/global/qttypetraits.h +++ b/src/corelib/global/qttypetraits.h @@ -45,6 +45,13 @@ noexcept(std::conjunction_v, return old; } +namespace QtPrivate { +// helper to be used to trigger a "dependent static_assert(false)" +// (for instance, in a final `else` branch of a `if constexpr`.) +template struct type_dependent_false : std::false_type {}; +template struct value_dependent_false : std::false_type {}; +} + QT_END_NAMESPACE #endif // QTTYPETRAITS_H diff --git a/src/corelib/tools/qflatmap_p.h b/src/corelib/tools/qflatmap_p.h index 6937d21544..68ab567439 100644 --- a/src/corelib/tools/qflatmap_p.h +++ b/src/corelib/tools/qflatmap_p.h @@ -848,7 +848,6 @@ public: size_type remove_if(Predicate pred) { const auto indirect_call_to_pred = [pred = std::move(pred)](iterator it) { - [[maybe_unused]] auto dependent_false = [](auto &&...) { return false; }; using Pair = decltype(*it); using K = decltype(it.key()); using V = decltype(it.value()); @@ -860,7 +859,7 @@ public: } else if constexpr (std::is_invocable_v && !std::is_invocable_v) { return pred(it.key()); } else { - static_assert(dependent_false(pred), + static_assert(QtPrivate::type_dependent_false(), "Don't know how to call the predicate.\n" "Options:\n" "- pred(*it)\n"