[PATCH, libstdc++] PR libstdc++/64656

Ville Voutilainen ville.voutilainen@gmail.com
Mon Jan 19 00:48:00 GMT 2015


On 19 January 2015 at 00:11, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
> This patch implements the free functions std::cbegin, std::cend,
> std::rbegin, std::rend, std::crbegin and std::crend, which were
> missing from our C++14 iterator library. I have run the added test
> manually but I haven't finished running the full testsuite yet. I will
> send the patch now anyway, if there are some funny line-number
> changes that need to be done elsewhere in the testsuite, I will
> know in a couple of hours which quantifies to tomorrow morning,
> since I won't stay up for it. ;)
>
> 2015-01-19  Ville Voutilainen  <ville.voutilainen@gmail.com>
>
>     PR libstdc++/64656
>     * include/bits/range_access.h (cbegin, cend, rbegin, rend,
>     crbegin, crend): New.
>     * testsuite/24_iterators/range_access_cpp14.cc: New.

Minor additions:
- add a comment to the #endif for the c++14 #if
- fix the doc comments to talk about reverse iterators

This passes the testsuite.
-------------- next part --------------
diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h
index fa18aa2..a5a511c 100644
--- a/libstdc++-v3/include/bits/range_access.h
+++ b/libstdc++-v3/include/bits/range_access.h
@@ -33,7 +33,7 @@
 #pragma GCC system_header
 
 #if __cplusplus >= 201103L
-
+#include <initializer_list>
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -97,6 +97,131 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     end(_Tp (&__arr)[_Nm])
     { return __arr + _Nm; }
 
+#if __cplusplus >= 201402L
+  /**
+   *  @brief  Return an iterator pointing to the first element of
+   *          the const container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline constexpr auto
+    cbegin(const _Container& __cont) noexcept(noexcept(std::begin(__cont)))
+      -> decltype(std::begin(__cont))
+    { return std::begin(__cont); }
+
+  /**
+   *  @brief  Return an iterator pointing to one past the last element of
+   *          the const container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline constexpr auto
+    cend(const _Container& __cont) noexcept(noexcept(std::end(__cont)))
+      -> decltype(std::end(__cont))
+    { return std::end(__cont); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing to the last element of
+   *          the container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline auto
+    rbegin(_Container& __cont) -> decltype(__cont.rbegin())
+    { return __cont.rbegin(); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing to the last element of
+   *          the const container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline auto
+    rbegin(const _Container& __cont) -> decltype(__cont.rbegin())
+    { return __cont.rbegin(); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing one past the first element of
+   *          the container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline auto
+    rend(_Container& __cont) -> decltype(__cont.rend())
+    { return __cont.rend(); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing one past the first element of
+   *          the const container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline auto
+    rend(const _Container& __cont) -> decltype(__cont.rend())
+    { return __cont.rend(); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing to the last element of
+   *          the array.
+   *  @param  __arr  Array.
+   */
+  template<class _Tp, size_t _Nm>
+    inline reverse_iterator<_Tp*>
+    rbegin(_Tp (&__arr)[_Nm])
+    { return reverse_iterator<_Tp*>(__arr + _Nm); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing one past the first element of
+   *          the array.
+   *  @param  __arr  Array.
+   */
+  template<class _Tp, size_t _Nm>
+    inline reverse_iterator<_Tp*>
+    rend(_Tp (&__arr)[_Nm])
+    { return reverse_iterator<_Tp*>(__arr); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing to the last element of
+   *          the initializer_list.
+   *  @param  __il  initializer_list.
+   */
+  template<class _Tp>
+    inline reverse_iterator<const _Tp*>
+    rbegin(initializer_list<_Tp> __il)
+    { return reverse_iterator<const _Tp*>(__il.end()); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing one past the first element of
+   *          the initializer_list.
+   *  @param  __il  initializer_list.
+   */
+  template<class _Tp>
+    inline reverse_iterator<const _Tp*>
+    rend(initializer_list<_Tp> __il)
+    { return reverse_iterator<const _Tp*>(__il.begin()); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing to the last element of
+   *          the const container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline auto
+    crbegin(const _Container& __cont) -> decltype(std::rbegin(__cont))
+    { return std::rbegin(__cont); }
+
+  /**
+   *  @brief  Return a reverse iterator pointing one past the first element of
+   *          the const container.
+   *  @param  __cont  Container.
+   */
+  template<class _Container>
+    inline auto
+    crend(const _Container& __cont) -> decltype(std::rend(__cont))
+    { return std::rend(__cont); }
+
+#endif // C++14
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
 
diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc
new file mode 100644
index 0000000..d618dba
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc
@@ -0,0 +1,52 @@
+// { dg-do run }
+// { dg-options "-std=gnu++14" }
+
+// Copyright (C) 2015 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/>.
+
+// 24.6.5, range access [iterator.range]
+
+#include <iterator>
+#include <vector>
+#include <cassert>
+
+int main()
+{
+  int i[1];
+  assert(std::cbegin(i) == i);
+  assert(std::cend(i) == i+1);
+  assert(std::rbegin(i) == std::reverse_iterator<int*>(i+1));
+  assert(std::rend(i) == std::reverse_iterator<int*>(i));
+  assert(std::crbegin(i) == std::reverse_iterator<int*>(i+1));
+  assert(std::crend(i) == std::reverse_iterator<int*>(i));
+
+  std::initializer_list<int> il{1};
+  assert(std::cbegin(il) == il.begin());
+  assert(std::cend(il) == il.end());
+  assert(std::rbegin(il) == std::reverse_iterator<const int*>(il.end()));
+  assert(std::rend(il) == std::reverse_iterator<const int*>(il.begin()));
+  assert(std::crbegin(il) == std::reverse_iterator<const int*>(il.end()));
+  assert(std::crend(il) == std::reverse_iterator<const int*>(il.begin()));
+
+  std::vector<int> v{1};
+  assert(std::cbegin(v) == v.cbegin());
+  assert(std::cend(v) == v.cend());
+  assert(std::rbegin(v) == v.rbegin());
+  assert(std::rend(v) == v.rend());
+  assert(std::crbegin(v) == v.crbegin());
+  assert(std::crend(v) == v.crend());
+}


More information about the Libstdc++ mailing list