1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <iterator>
10 
11 // reverse_iterator
12 
13 // reference operator*() const; // constexpr in C++17
14 
15 // Be sure to respect LWG 198:
16 //    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#198
17 // LWG 198 was superseded by LWG 2360
18 //    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2360
19 
20 #include <iterator>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "test_iterators.h"
25 
26 class A
27 {
28     int data_;
29 public:
30     A() : data_(1) {}
31     A(const A&) = default;
32     A& operator=(const A&) = default;
33     ~A() {data_ = -1;}
34 
35     friend bool operator==(const A& x, const A& y)
36         {return x.data_ == y.data_;}
37 };
38 
39 template <class It>
40 void
41 test(It i, typename std::iterator_traits<It>::value_type x)
42 {
43     std::reverse_iterator<It> r(i);
44     assert(*r == x);
45 }
46 
47 int main(int, char**)
48 {
49     A a;
50     test(&a+1, A());
51     test(random_access_iterator<A*>(&a + 1), A());
52 #if TEST_STD_VER >= 20
53     test(cpp20_random_access_iterator<A*>(&a + 1), A());
54 #endif
55 
56 #if TEST_STD_VER > 14
57     {
58         constexpr const char *p = "123456789";
59         typedef std::reverse_iterator<const char *> RI;
60         constexpr RI it1 = std::make_reverse_iterator(p+1);
61         constexpr RI it2 = std::make_reverse_iterator(p+2);
62         static_assert(*it1 == p[0], "");
63         static_assert(*it2 == p[1], "");
64     }
65 #endif
66 
67   return 0;
68 }
69