From f879e041339ee414ddb9514a0883baa66ce34f6d Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 22 Jul 2021 19:07:17 +0430 Subject: [PATCH] AK: Add a concept for iterable containers This concept describes a type with a begin()/end() pair that can function as an iterator, given the following criteria: - The return type of begin() is comparable with the return type of end(), and the comparison (with operator!=) yields a bool - The object returned from begin() can be pre-incremented - The iterator has an operator*() implementation --- AK/Concepts.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/AK/Concepts.h b/AK/Concepts.h index ced4b291424..697a8f1ec0b 100644 --- a/AK/Concepts.h +++ b/AK/Concepts.h @@ -51,6 +51,21 @@ concept IteratorFunction = requires(Func func, Args... args) } -> SameAs; }; + +template +concept IteratorPairWith = requires(T it, EndT end) +{ + *it; + { it != end } -> SameAs; + ++it; +}; + +template +concept IterableContainer = requires +{ + { declval().begin() } -> IteratorPairWith().end())>; +}; + // clang-format on } @@ -58,7 +73,9 @@ using AK::Concepts::Arithmetic; using AK::Concepts::Enum; using AK::Concepts::FloatingPoint; using AK::Concepts::Integral; +using AK::Concepts::IterableContainer; using AK::Concepts::IteratorFunction; +using AK::Concepts::IteratorPairWith; using AK::Concepts::Signed; using AK::Concepts::Unsigned; using AK::Concepts::VoidFunction;