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