1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <iterator> 11 12 // reverse_iterator 13 14 // pointer operator->() const; 15 16 // Be sure to respect LWG 198: 17 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#198 18 // LWG 198 was superseded by LWG 2360 19 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2360 20 21 22 #include <iterator> 23 #include <list> 24 #include <cassert> 25 26 class A 27 { 28 int data_; 29 public: A()30 A() : data_(1) {} ~A()31 ~A() {data_ = -1;} 32 get() const33 int get() const {return data_;} 34 operator ==(const A & x,const A & y)35 friend bool operator==(const A& x, const A& y) 36 {return x.data_ == y.data_;} 37 }; 38 39 template <class It> 40 void test(It i,typename std::iterator_traits<It>::value_type x)41test(It i, typename std::iterator_traits<It>::value_type x) 42 { 43 std::reverse_iterator<It> r(i); 44 assert(r->get() == x.get()); 45 } 46 47 class B 48 { 49 int data_; 50 public: B(int d=1)51 B(int d=1) : data_(d) {} ~B()52 ~B() {data_ = -1;} 53 get() const54 int get() const {return data_;} 55 operator ==(const B & x,const B & y)56 friend bool operator==(const B& x, const B& y) 57 {return x.data_ == y.data_;} operator &() const58 const B *operator&() const { return nullptr; } operator &()59 B *operator&() { return nullptr; } 60 }; 61 main()62int main() 63 { 64 A a; 65 test(&a+1, A()); 66 67 { 68 std::list<B> l; 69 l.push_back(B(0)); 70 l.push_back(B(1)); 71 l.push_back(B(2)); 72 73 { 74 std::list<B>::const_iterator i = l.begin(); 75 assert ( i->get() == 0 ); ++i; 76 assert ( i->get() == 1 ); ++i; 77 assert ( i->get() == 2 ); ++i; 78 assert ( i == l.end ()); 79 } 80 81 { 82 std::list<B>::const_reverse_iterator ri = l.rbegin(); 83 assert ( ri->get() == 2 ); ++ri; 84 assert ( ri->get() == 1 ); ++ri; 85 assert ( ri->get() == 0 ); ++ri; 86 assert ( ri == l.rend ()); 87 } 88 } 89 } 90