diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-12-05 12:59:10 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-12-06 06:30:55 +0100 |
commit | 21425c05e0a91a2b16503958bbf0201c108df967 (patch) | |
tree | 7d2cd32d337b97a9aca24e523daf5e252fd0a027 /src/corelib | |
parent | 3a449bbb69c9a3c3a5bc6a052f2de98ab79be7e9 (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/global/qttypetraits.h | 7 | ||||
-rw-r--r-- | 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<std::is_nothrow_move_constructible<T>, 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 <typename T> struct type_dependent_false : std::false_type {}; +template <auto T> 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<P, K> && !std::is_invocable_v<P, Pair>) { return pred(it.key()); } else { - static_assert(dependent_false(pred), + static_assert(QtPrivate::type_dependent_false<Predicate>(), "Don't know how to call the predicate.\n" "Options:\n" "- pred(*it)\n" |