From 02d3b4dd6448245c9e0e9e5146671c1e25ab58b6 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 22 Dec 2017 12:34:56 +1100 Subject: [PATCH] iterator: add unequal_iterator unequal_iterator is a forward iterator that never successfully compares for equality with other types. useful as the end iterator when one has an output iterator as the begin iterator of a view. --- iterator.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/iterator.hpp b/iterator.hpp index c288e7af..0d4428e3 100644 --- a/iterator.hpp +++ b/iterator.hpp @@ -438,6 +438,84 @@ namespace util { discard_iterator operator++ (int) { return *this; } discard_iterator& operator* ( ) { return *this; } }; + + + /////////////////////////////////////////////////////////////////////////// + /// an iterator that can be infinitely incremented but never assigned. + /// + /// useful for iterator ranges where the begin iterator is an output + /// iterator and hence never reaches an end point (and where we don't want + /// to engineer the client code to account for this). + template < + typename ValueT, + typename CategoryT, + typename DistanceT, + typename PointerT, + typename ReferenceT + > + struct unequal_iterator { + using value_type = ValueT; + using iterator_category = CategoryT; + using difference_type = DistanceT; + using pointer = PointerT; + using reference = ReferenceT; + + unequal_iterator& operator++ ( ) { return *this; } + unequal_iterator operator++ (int) { return *this; } + }; + + + //------------------------------------------------------------------------- + template + auto + make_unequal_iterator (const ContainerT&) + { + using t = typename std::iterator_traits; + + return unequal_iterator< + typename t::value_type, + typename t::iterator_category, + typename t::difference_type, + typename t::pointer, + typename t::reference + > {}; + }; + + + //------------------------------------------------------------------------- + template < + typename OtherT, + + typename ValueT, + typename CategoryT, + typename DistanceT, + typename PointerT, + typename ReferenceT> + constexpr bool + operator== ( + const unequal_iterator&, + const OtherT& + ) { + return false; + } + + + //------------------------------------------------------------------------- + template < + typename OtherT, + + typename ValueT, + typename CategoryT, + typename DistanceT, + typename PointerT, + typename ReferenceT> + constexpr bool + operator== ( + const OtherT&, + const unequal_iterator& + ) { + return false; + } }; #endif