From 8bae22b708305524126b9462999890242c7e809e Mon Sep 17 00:00:00 2001 From: Ville Voutilainen Date: Mon, 19 Jan 2015 16:37:03 +0200 Subject: [PATCH] range_access.h (begin, end): Use _GLIBCXX14_CONSTEXPR on overloads for arrays. 2015-01-19 Ville Voutilainen Jonathan Wakely * include/bits/range_access.h (begin, end): Use _GLIBCXX14_CONSTEXPR on overloads for arrays. (cbegin, cend, rbegin, rend, crbegin, crend): New. * testsuite/24_iterators/range_access_cpp14.cc: New. Co-Authored-By: Jonathan Wakely From-SVN: r219846 --- libstdc++-v3/ChangeLog | 8 ++ libstdc++-v3/include/bits/range_access.h | 131 +++++++++++++++++- .../24_iterators/range_access_cpp14.cc | 82 +++++++++++ 3 files changed, 218 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 52f77b880ec..23969cb42c7 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2015-01-19 Ville Voutilainen + Jonathan Wakely + + * include/bits/range_access.h (begin, end): Use _GLIBCXX14_CONSTEXPR + on overloads for arrays. + (cbegin, cend, rbegin, rend, crbegin, crend): New. + * testsuite/24_iterators/range_access_cpp14.cc: New. + 2015-01-18 Jonathan Wakely PR libstdc++/64646 diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h index fa18aa280cc..510c0b11e5e 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 namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -83,7 +83,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __arr Array. */ template - inline _Tp* + inline _GLIBCXX14_CONSTEXPR _Tp* begin(_Tp (&__arr)[_Nm]) { return __arr; } @@ -93,10 +93,135 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __arr Array. */ template - inline _Tp* + inline _GLIBCXX14_CONSTEXPR _Tp* 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + inline reverse_iterator + rbegin(initializer_list<_Tp> __il) + { return reverse_iterator(__il.end()); } + + /** + * @brief Return a reverse iterator pointing one past the first element of + * the initializer_list. + * @param __il initializer_list. + */ + template + inline reverse_iterator + rend(initializer_list<_Tp> __il) + { return reverse_iterator(__il.begin()); } + + /** + * @brief Return a reverse iterator pointing to the last element of + * the const container. + * @param __cont Container. + */ + template + 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 + 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 00000000000..a75e04cef9a --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc @@ -0,0 +1,82 @@ +// { 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 +// . + +// 24.6.5, range access [iterator.range] + +#include +#include +#include + +bool test __attribute__((unused)) = true; + +void +test01() +{ + int i[1]; + VERIFY(std::cbegin(i) == i); + VERIFY(std::cend(i) == i+1); + VERIFY(std::rbegin(i) == std::reverse_iterator(i+1)); + VERIFY(std::rend(i) == std::reverse_iterator(i)); + VERIFY(std::crbegin(i) == std::reverse_iterator(i+1)); + VERIFY(std::crend(i) == std::reverse_iterator(i)); +} + +void +test02() +{ + static int i[1]; + constexpr auto b __attribute__((unused)) = std::begin(i); + constexpr auto e __attribute__((unused)) = std::end(i); + constexpr auto cb __attribute__((unused)) = std::cbegin(i); + constexpr auto ce __attribute__((unused)) = std::cend(i); +} + +int +test03() +{ + std::initializer_list il{1}; + VERIFY(std::cbegin(il) == il.begin()); + VERIFY(std::cend(il) == il.end()); + VERIFY(std::rbegin(il) == std::reverse_iterator(il.end())); + VERIFY(std::rend(il) == std::reverse_iterator(il.begin())); + VERIFY(std::crbegin(il) == std::reverse_iterator(il.end())); + VERIFY(std::crend(il) == std::reverse_iterator(il.begin())); +} + +int +test04() +{ + std::vector v{1}; + VERIFY(std::cbegin(v) == v.cbegin()); + VERIFY(std::cend(v) == v.cend()); + VERIFY(std::rbegin(v) == v.rbegin()); + VERIFY(std::rend(v) == v.rend()); + VERIFY(std::crbegin(v) == v.crbegin()); + VERIFY(std::crend(v) == v.crend()); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); +} -- 2.30.2