| 1 |
// Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++11 } } |
| 19 |
|
| 20 |
// PR libstdc++/86734 |
| 21 |
// LWG 1052. reverse_iterator::operator-> should also support smart pointers |
| 22 |
// LWG 2775. reverse_iterator is does not compile for fancy pointers |
| 23 |
|
| 24 |
#include <iterator> |
| 25 |
#include <testsuite_hooks.h> |
| 26 |
|
| 27 |
void |
| 28 |
test01() |
| 29 |
{ |
| 30 |
// Example 1 from LWG 1052 |
| 31 |
|
| 32 |
struct X { int m; }; |
| 33 |
|
| 34 |
static X x; |
| 35 |
|
| 36 |
struct IterX { |
| 37 |
typedef std::bidirectional_iterator_tag iterator_category; |
| 38 |
typedef X& reference; |
| 39 |
struct pointer |
| 40 |
{ |
| 41 |
pointer(X& v) : value(v) {} |
| 42 |
X& value; |
| 43 |
X* operator->() const {return &value;} |
| 44 |
}; |
| 45 |
typedef std::ptrdiff_t difference_type; |
| 46 |
typedef X value_type; |
| 47 |
// additional iterator requirements not important for this issue |
| 48 |
|
| 49 |
reference operator*() const { return x; } |
| 50 |
pointer operator->() const { return pointer(x); } |
| 51 |
IterX& operator--() {return *this;} |
| 52 |
|
| 53 |
}; |
| 54 |
|
| 55 |
std::reverse_iterator<IterX> ix; |
| 56 |
VERIFY( &ix->m == &(*ix).m ); |
| 57 |
} |
| 58 |
|
| 59 |
void |
| 60 |
test02() |
| 61 |
{ |
| 62 |
// Example 2 from LWG 1052 |
| 63 |
|
| 64 |
struct P { |
| 65 |
P() : first(10), second(20.0) { } |
| 66 |
int first; |
| 67 |
double second; |
| 68 |
}; |
| 69 |
P op; |
| 70 |
std::reverse_iterator<P*> ri(&op + 1); |
| 71 |
VERIFY( ri->first == 10 ); |
| 72 |
} |
| 73 |
|
| 74 |
// N.B. Example 3 from LWG 1052 isn't expected to work, |
| 75 |
// because a caching iterator like IterX is not a forward iterator. |
| 76 |
|
| 77 |
int |
| 78 |
main() |
| 79 |
{ |
| 80 |
test01(); |
| 81 |
test02(); |
| 82 |
} |