2018-07-30 Jonathan Wakely <jwakely@redhat.com>
+ PR libstdc++/86734
+ * include/bits/stl_iterator.h (reverse_iterator::operator->): Call
+ _S_to_pointer (LWG 1052, LWG 2118).
+ (reverse_iterator::_S_to_pointer): Define overloaded helper functions.
+ * testsuite/24_iterators/reverse_iterator/dr1052.cc: New test.
+ * testsuite/24_iterators/reverse_iterator/dr2188.cc: New test.
+
* libsupc++/new_opa.cc (operator new(size_t, align_val_t)): Add
workaround for aligned_alloc bug on AIX.
* testsuite/18_support/new_aligned.cc: New test.
*/
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 235 No specification of default ctor for reverse_iterator
+ // 1012. reverse_iterator default ctor should value initialize
_GLIBCXX17_CONSTEXPR
reverse_iterator() : current() { }
*/
_GLIBCXX17_CONSTEXPR pointer
operator->() const
- { return &(operator*()); }
+ {
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 1052. operator-> should also support smart pointers
+ _Iterator __tmp = current;
+ --__tmp;
+ return _S_to_pointer(__tmp);
+ }
/**
* @return @c *this
_GLIBCXX17_CONSTEXPR reference
operator[](difference_type __n) const
{ return *(*this + __n); }
+
+ private:
+ template<typename _Tp>
+ static _GLIBCXX17_CONSTEXPR _Tp*
+ _S_to_pointer(_Tp* __p)
+ { return __p; }
+
+ template<typename _Tp>
+ static _GLIBCXX17_CONSTEXPR pointer
+ _S_to_pointer(_Tp __t)
+ { return __t.operator->(); }
};
//@{
--- /dev/null
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// PR libstdc++/86734
+// LWG 1052. reverse_iterator::operator-> should also support smart pointers
+// LWG 2775. reverse_iterator is does not compile for fancy pointers
+
+#include <iterator>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ // Example 1 from LWG 1052
+
+ struct X { int m; };
+
+ static X x;
+
+ struct IterX {
+ typedef std::bidirectional_iterator_tag iterator_category;
+ typedef X& reference;
+ struct pointer
+ {
+ pointer(X& v) : value(v) {}
+ X& value;
+ X* operator->() const {return &value;}
+ };
+ typedef std::ptrdiff_t difference_type;
+ typedef X value_type;
+ // additional iterator requirements not important for this issue
+
+ reference operator*() const { return x; }
+ pointer operator->() const { return pointer(x); }
+ IterX& operator--() {return *this;}
+
+ };
+
+ std::reverse_iterator<IterX> ix;
+ VERIFY( &ix->m == &(*ix).m );
+}
+
+void
+test02()
+{
+ // Example 2 from LWG 1052
+
+ struct P {
+ P() : first(10), second(20.0) { }
+ int first;
+ double second;
+ };
+ P op;
+ std::reverse_iterator<P*> ri(&op + 1);
+ VERIFY( ri->first == 10 );
+}
+
+// N.B. Example 3 from LWG 1052 isn't expected to work,
+// because a caching iterator like IterX is not a forward iterator.
+
+int
+main()
+{
+ test01();
+ test02();
+}
--- /dev/null
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+// PR libstdc++/86734
+
+#include <iterator>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ // LWG DR 2188
+ // Reverse iterator does not fully support targets that overload operator&
+ struct X {
+ int val;
+ int* operator&() { return &val; }
+ const int* operator&() const { return &val; }
+ };
+
+ X x[2] = { {1}, {2} };
+ std::reverse_iterator<X*> rev(x+2);
+ VERIFY( rev->val == 2 );
+ ++rev;
+ VERIFY( rev->val == 1 );
+}
+
+int
+main()
+{
+ test01();
+}