From 077dd8f1b4f984ebd4bc8d5e50d1ff584385608a Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 30 Apr 2014 00:49:32 +0000 Subject: [PATCH] [ADT] Provide some helpful static_asserts for using operations of the wrong iterator category. These aren't comprehensive, but they have caught the common cases for me and produce much nicer errors. llvm-svn: 207601 --- include/llvm/ADT/iterator.h | 64 ++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/include/llvm/ADT/iterator.h b/include/llvm/ADT/iterator.h index 8d6a8eb163f..56041dbb106 100644 --- a/include/llvm/ADT/iterator.h +++ b/include/llvm/ADT/iterator.h @@ -35,18 +35,36 @@ namespace llvm { template -struct iterator_facade_base - : std::iterator { +class iterator_facade_base + : public std::iterator { +protected: + enum { + IsRandomAccess = + std::is_base_of::value, + IsBidirectional = + std::is_base_of::value, + }; + +public: DerivedT operator+(DifferenceTypeT n) const { + static_assert( + IsRandomAccess, + "The '+' operator is only defined for random access iterators."); DerivedT tmp = *static_cast(this); tmp += n; return tmp; } friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) { + static_assert( + IsRandomAccess, + "The '+' operator is only defined for random access iterators."); return i + n; } DerivedT operator-(DifferenceTypeT n) const { + static_assert( + IsRandomAccess, + "The '-' operator is only defined for random access iterators."); DerivedT tmp = *static_cast(this); tmp -= n; return tmp; @@ -61,9 +79,15 @@ struct iterator_facade_base return tmp; } DerivedT &operator--() { + static_assert( + IsBidirectional, + "The decrement operator is only defined for bidirectional iterators."); return static_cast(this)->operator-=(1); } DerivedT operator--(int) { + static_assert( + IsBidirectional, + "The decrement operator is only defined for bidirectional iterators."); DerivedT tmp = *static_cast(this); --*static_cast(this); return tmp; @@ -74,13 +98,22 @@ struct iterator_facade_base } bool operator>(const DerivedT &RHS) const { + static_assert( + IsRandomAccess, + "Relational operators are only defined for random access iterators."); return !static_cast(this)->operator<(RHS) && !static_cast(this)->operator==(RHS); } bool operator<=(const DerivedT &RHS) const { + static_assert( + IsRandomAccess, + "Relational operators are only defined for random access iterators."); return !static_cast(this)->operator>(RHS); } bool operator>=(const DerivedT &RHS) const { + static_assert( + IsRandomAccess, + "Relational operators are only defined for random access iterators."); return !static_cast(this)->operator<(RHS); } @@ -88,6 +121,8 @@ struct iterator_facade_base return &static_cast(this)->operator*(); } ReferenceT operator[](DifferenceTypeT n) const { + static_assert(IsRandomAccess, + "Subscripting is only defined for random access iterators."); return *static_cast(this)->operator+(n); } }; @@ -131,15 +166,26 @@ public: typedef DifferenceTypeT difference_type; DerivedT &operator+=(difference_type n) { + static_assert( + BaseT::IsRandomAccess, + "The '+=' operator is only defined for random access iterators."); I += n; return *static_cast(this); } DerivedT &operator-=(difference_type n) { + static_assert( + BaseT::IsRandomAccess, + "The '-=' operator is only defined for random access iterators."); I -= n; return *static_cast(this); } using BaseT::operator-; - difference_type operator-(const DerivedT &RHS) const { return I - RHS.I; } + difference_type operator-(const DerivedT &RHS) const { + static_assert( + BaseT::IsRandomAccess, + "The '-' operator is only defined for random access iterators."); + return I - RHS.I; + } // We have to explicitly provide ++ and -- rather than letting the facade // forward to += because WrappedIteratorT might not support +=. @@ -150,12 +196,20 @@ public: } using BaseT::operator--; DerivedT &operator--() { + static_assert( + BaseT::IsBidirectional, + "The decrement operator is only defined for bidirectional iterators."); --I; return *static_cast(this); } bool operator==(const DerivedT &RHS) const { return I == RHS.I; } - bool operator<(const DerivedT &RHS) const { return I < RHS.I; } + bool operator<(const DerivedT &RHS) const { + static_assert( + BaseT::IsRandomAccess, + "Relational operators are only defined for random access iterators."); + return I < RHS.I; + } ReferenceT operator*() const { return *I; } };