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 // constexpr pointer operator->() const;
15 //
16 // constexpr in C++17
17
18 // Be sure to respect LWG 198:
19 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#198
20 // LWG 198 was superseded by LWG 2360
21 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2360
22
23
24 #include <iterator>
25 #include <list>
26 #include <cassert>
27
28 #include "test_macros.h"
29
30 class A
31 {
32 int data_;
33 public:
A()34 A() : data_(1) {}
~A()35 ~A() {data_ = -1;}
36
get() const37 int get() const {return data_;}
38
operator ==(const A & x,const A & y)39 friend bool operator==(const A& x, const A& y)
40 {return x.data_ == y.data_;}
41 };
42
43 template <class It>
44 void
test(It i,typename std::iterator_traits<It>::value_type x)45 test(It i, typename std::iterator_traits<It>::value_type x)
46 {
47 std::reverse_iterator<It> r(i);
48 assert(r->get() == x.get());
49 }
50
51 class B
52 {
53 int data_;
54 public:
B(int d=1)55 B(int d=1) : data_(d) {}
~B()56 ~B() {data_ = -1;}
57
get() const58 int get() const {return data_;}
59
operator ==(const B & x,const B & y)60 friend bool operator==(const B& x, const B& y)
61 {return x.data_ == y.data_;}
operator &() const62 const B *operator&() const { return nullptr; }
operator &()63 B *operator&() { return nullptr; }
64 };
65
66 class C
67 {
68 int data_;
69 public:
C()70 TEST_CONSTEXPR C() : data_(1) {}
71
get() const72 TEST_CONSTEXPR int get() const {return data_;}
73
operator ==(const C & x,const C & y)74 friend TEST_CONSTEXPR bool operator==(const C& x, const C& y)
75 {return x.data_ == y.data_;}
76 };
77
78 TEST_CONSTEXPR C gC;
79
main()80 int main()
81 {
82 A a;
83 test(&a+1, A());
84
85 {
86 std::list<B> l;
87 l.push_back(B(0));
88 l.push_back(B(1));
89 l.push_back(B(2));
90
91 {
92 std::list<B>::const_iterator i = l.begin();
93 assert ( i->get() == 0 ); ++i;
94 assert ( i->get() == 1 ); ++i;
95 assert ( i->get() == 2 ); ++i;
96 assert ( i == l.end ());
97 }
98
99 {
100 std::list<B>::const_reverse_iterator ri = l.rbegin();
101 assert ( ri->get() == 2 ); ++ri;
102 assert ( ri->get() == 1 ); ++ri;
103 assert ( ri->get() == 0 ); ++ri;
104 assert ( ri == l.rend ());
105 }
106 }
107
108 #if TEST_STD_VER > 14
109 {
110 typedef std::reverse_iterator<const C *> RI;
111 constexpr RI it1 = std::make_reverse_iterator(&gC+1);
112
113 static_assert(it1->get() == gC.get(), "");
114 }
115 #endif
116 {
117 ((void)gC);
118 }
119 }
120