PR libstdc++/86734 make reverse_iterator::operator-> more robust
[official-gcc.git] / libstdc++-v3 / testsuite / 24_iterators / reverse_iterator / dr1052.cc
blob2704010a083a2ab65c7e1696c46f5f077fc3e419
1 // Copyright (C) 2018 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.
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.
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/>.
18 // { dg-do run { target c++11 } }
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
24 #include <iterator>
25 #include <testsuite_hooks.h>
27 void
28 test01()
30 // Example 1 from LWG 1052
32 struct X { int m; };
34 static X x;
36 struct IterX {
37 typedef std::bidirectional_iterator_tag iterator_category;
38 typedef X& reference;
39 struct pointer
41 pointer(X& v) : value(v) {}
42 X& value;
43 X* operator->() const {return &value;}
45 typedef std::ptrdiff_t difference_type;
46 typedef X value_type;
47 // additional iterator requirements not important for this issue
49 reference operator*() const { return x; }
50 pointer operator->() const { return pointer(x); }
51 IterX& operator--() {return *this;}
55 std::reverse_iterator<IterX> ix;
56 VERIFY( &ix->m == &(*ix).m );
59 void
60 test02()
62 // Example 2 from LWG 1052
64 struct P {
65 P() : first(10), second(20.0) { }
66 int first;
67 double second;
69 P op;
70 std::reverse_iterator<P*> ri(&op + 1);
71 VERIFY( ri->first == 10 );
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.
77 int
78 main()
80 test01();
81 test02();