Commit 06db9920 by Jonathan Wakely Committed by Jonathan Wakely

Add constexpr to <iterator> and <array> for C++17

	* include/bits/c++config (_GLIBCXX17_CONSTEXPR): Define.
	* include/bits/range_access.h (begin, end, rbegin, rend, crbegin)
	(crend): Add _GLIBCXX17_CONSTEXPR as per P0031R0.
	* include/bits/stl_iterator.h (reverse_iterator, move_iterator)
	(__make_reverse_iterator, make_reverse_iterator, make_move_iterator):
	Likewise.
	* include/bits/stl_iterator_base_funcs.h (__distance, __advance): Add
	_GLIBCXX14_CONSTEXPR.
	(distance, advance, next, prev): Add _GLIBCXX17_CONSTEXPR.
	* include/std/array (array::begin, array::end, array::rbegin)
	(array::rend, array::cbegin, array:cend, array::crbegin)
	(array::crend, array::operator[], array::at, array::front)
	(array::back, array::data): Likewise.
	* testsuite/24_iterators/headers/iterator/range_access.cc: Replace
	with separate tests for C++11, C++14, and C++17.
	* testsuite/24_iterators/headers/iterator/range_access_c++11.cc: New.
	* testsuite/24_iterators/headers/iterator/range_access_c++14.cc: New.
	* testsuite/24_iterators/headers/iterator/range_access_c++17.cc: New.

From-SVN: r239690
parent 1135a133
2016-08-23 Jonathan Wakely <jwakely@redhat.com>
* include/bits/c++config (_GLIBCXX17_CONSTEXPR): Define.
* include/bits/range_access.h (begin, end, rbegin, rend, crbegin)
(crend): Add _GLIBCXX17_CONSTEXPR as per P0031R0.
* include/bits/stl_iterator.h (reverse_iterator, move_iterator)
(__make_reverse_iterator, make_reverse_iterator, make_move_iterator):
Likewise.
* include/bits/stl_iterator_base_funcs.h (__distance, __advance): Add
_GLIBCXX14_CONSTEXPR.
(distance, advance, next, prev): Add _GLIBCXX17_CONSTEXPR.
* include/std/array (array::begin, array::end, array::rbegin)
(array::rend, array::cbegin, array:cend, array::crbegin)
(array::crend, array::operator[], array::at, array::front)
(array::back, array::data): Likewise.
* testsuite/24_iterators/headers/iterator/range_access.cc: Replace
with separate tests for C++11, C++14, and C++17.
* testsuite/24_iterators/headers/iterator/range_access_c++11.cc: New.
* testsuite/24_iterators/headers/iterator/range_access_c++14.cc: New.
* testsuite/24_iterators/headers/iterator/range_access_c++17.cc: New.
2016-08-22 Tim Shen <timshen@google.com>
Split _M_dfs() into smaller functions.
......
......@@ -111,6 +111,14 @@
# endif
#endif
#ifndef _GLIBCXX17_CONSTEXPR
# if __cplusplus > 201402L
# define _GLIBCXX17_CONSTEXPR constexpr
# else
# define _GLIBCXX17_CONSTEXPR
# endif
#endif
// Macro for noexcept, to support in mixed 03/0x mode.
#ifndef _GLIBCXX_NOEXCEPT
# if __cplusplus >= 201103L
......
......@@ -48,7 +48,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
begin(_Container& __cont) -> decltype(__cont.begin())
{ return __cont.begin(); }
......@@ -58,7 +58,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
begin(const _Container& __cont) -> decltype(__cont.begin())
{ return __cont.begin(); }
......@@ -68,7 +68,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
end(_Container& __cont) -> decltype(__cont.end())
{ return __cont.end(); }
......@@ -78,7 +78,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
end(const _Container& __cont) -> decltype(__cont.end())
{ return __cont.end(); }
......@@ -138,7 +138,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
rbegin(_Container& __cont) -> decltype(__cont.rbegin())
{ return __cont.rbegin(); }
......@@ -148,7 +148,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
rbegin(const _Container& __cont) -> decltype(__cont.rbegin())
{ return __cont.rbegin(); }
......@@ -158,7 +158,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
rend(_Container& __cont) -> decltype(__cont.rend())
{ return __cont.rend(); }
......@@ -168,7 +168,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
rend(const _Container& __cont) -> decltype(__cont.rend())
{ return __cont.rend(); }
......@@ -178,7 +178,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __arr Array.
*/
template<typename _Tp, size_t _Nm>
inline reverse_iterator<_Tp*>
inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
rbegin(_Tp (&__arr)[_Nm])
{ return reverse_iterator<_Tp*>(__arr + _Nm); }
......@@ -188,7 +188,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __arr Array.
*/
template<typename _Tp, size_t _Nm>
inline reverse_iterator<_Tp*>
inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
rend(_Tp (&__arr)[_Nm])
{ return reverse_iterator<_Tp*>(__arr); }
......@@ -198,7 +198,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __il initializer_list.
*/
template<typename _Tp>
inline reverse_iterator<const _Tp*>
inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
rbegin(initializer_list<_Tp> __il)
{ return reverse_iterator<const _Tp*>(__il.end()); }
......@@ -208,7 +208,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __il initializer_list.
*/
template<typename _Tp>
inline reverse_iterator<const _Tp*>
inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
rend(initializer_list<_Tp> __il)
{ return reverse_iterator<const _Tp*>(__il.begin()); }
......@@ -218,7 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
crbegin(const _Container& __cont) -> decltype(std::rbegin(__cont))
{ return std::rbegin(__cont); }
......@@ -228,7 +228,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __cont Container.
*/
template<typename _Container>
inline auto
inline _GLIBCXX17_CONSTEXPR auto
crend(const _Container& __cont) -> decltype(std::rend(__cont))
{ return std::rend(__cont); }
......
......@@ -75,7 +75,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
_GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
inline _GLIBCXX14_CONSTEXPR
typename iterator_traits<_InputIterator>::difference_type
__distance(_InputIterator __first, _InputIterator __last,
input_iterator_tag)
{
......@@ -92,7 +93,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _RandomAccessIterator>
inline typename iterator_traits<_RandomAccessIterator>::difference_type
inline _GLIBCXX14_CONSTEXPR
typename iterator_traits<_RandomAccessIterator>::difference_type
__distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
random_access_iterator_tag)
{
......@@ -131,7 +133,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* and are constant time. For other %iterator classes they are linear time.
*/
template<typename _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
inline _GLIBCXX17_CONSTEXPR
typename iterator_traits<_InputIterator>::difference_type
distance(_InputIterator __first, _InputIterator __last)
{
// concept requirements -- taken care of in __distance
......@@ -140,7 +143,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _InputIterator, typename _Distance>
inline void
inline _GLIBCXX14_CONSTEXPR void
__advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
{
// concept requirements
......@@ -151,7 +154,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _BidirectionalIterator, typename _Distance>
inline void
inline _GLIBCXX14_CONSTEXPR void
__advance(_BidirectionalIterator& __i, _Distance __n,
bidirectional_iterator_tag)
{
......@@ -167,7 +170,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _RandomAccessIterator, typename _Distance>
inline void
inline _GLIBCXX14_CONSTEXPR void
__advance(_RandomAccessIterator& __i, _Distance __n,
random_access_iterator_tag)
{
......@@ -190,7 +193,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* and are constant time. For other %iterator classes they are linear time.
*/
template<typename _InputIterator, typename _Distance>
inline void
inline _GLIBCXX17_CONSTEXPR void
advance(_InputIterator& __i, _Distance __n)
{
// concept requirements -- taken care of in __advance
......@@ -201,7 +204,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#if __cplusplus >= 201103L
template<typename _ForwardIterator>
inline _ForwardIterator
inline _GLIBCXX17_CONSTEXPR _ForwardIterator
next(_ForwardIterator __x, typename
iterator_traits<_ForwardIterator>::difference_type __n = 1)
{
......@@ -213,7 +216,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _BidirectionalIterator>
inline _BidirectionalIterator
inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator
prev(_BidirectionalIterator __x, typename
iterator_traits<_BidirectionalIterator>::difference_type __n = 1)
{
......
......@@ -122,51 +122,51 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{ std::swap_ranges(begin(), end(), __other.begin()); }
// Iterators.
iterator
_GLIBCXX17_CONSTEXPR iterator
begin() noexcept
{ return iterator(data()); }
const_iterator
_GLIBCXX17_CONSTEXPR const_iterator
begin() const noexcept
{ return const_iterator(data()); }
iterator
_GLIBCXX17_CONSTEXPR iterator
end() noexcept
{ return iterator(data() + _Nm); }
const_iterator
_GLIBCXX17_CONSTEXPR const_iterator
end() const noexcept
{ return const_iterator(data() + _Nm); }
reverse_iterator
_GLIBCXX17_CONSTEXPR reverse_iterator
rbegin() noexcept
{ return reverse_iterator(end()); }
const_reverse_iterator
_GLIBCXX17_CONSTEXPR const_reverse_iterator
rbegin() const noexcept
{ return const_reverse_iterator(end()); }
reverse_iterator
_GLIBCXX17_CONSTEXPR reverse_iterator
rend() noexcept
{ return reverse_iterator(begin()); }
const_reverse_iterator
_GLIBCXX17_CONSTEXPR const_reverse_iterator
rend() const noexcept
{ return const_reverse_iterator(begin()); }
const_iterator
_GLIBCXX17_CONSTEXPR const_iterator
cbegin() const noexcept
{ return const_iterator(data()); }
const_iterator
_GLIBCXX17_CONSTEXPR const_iterator
cend() const noexcept
{ return const_iterator(data() + _Nm); }
const_reverse_iterator
_GLIBCXX17_CONSTEXPR const_reverse_iterator
crbegin() const noexcept
{ return const_reverse_iterator(end()); }
const_reverse_iterator
_GLIBCXX17_CONSTEXPR const_reverse_iterator
crend() const noexcept
{ return const_reverse_iterator(begin()); }
......@@ -181,7 +181,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
empty() const noexcept { return size() == 0; }
// Element access.
reference
_GLIBCXX17_CONSTEXPR reference
operator[](size_type __n) noexcept
{ return _AT_Type::_S_ref(_M_elems, __n); }
......@@ -189,7 +189,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
operator[](size_type __n) const noexcept
{ return _AT_Type::_S_ref(_M_elems, __n); }
reference
_GLIBCXX17_CONSTEXPR reference
at(size_type __n)
{
if (__n >= _Nm)
......@@ -211,7 +211,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
_AT_Type::_S_ref(_M_elems, 0));
}
reference
_GLIBCXX17_CONSTEXPR reference
front() noexcept
{ return *begin(); }
......@@ -219,7 +219,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
front() const noexcept
{ return _AT_Type::_S_ref(_M_elems, 0); }
reference
_GLIBCXX17_CONSTEXPR reference
back() noexcept
{ return _Nm ? *(end() - 1) : *end(); }
......@@ -230,11 +230,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
: _AT_Type::_S_ref(_M_elems, 0);
}
pointer
_GLIBCXX17_CONSTEXPR pointer
data() noexcept
{ return _AT_Type::_S_ptr(_M_elems); }
const_pointer
_GLIBCXX17_CONSTEXPR const_pointer
data() const noexcept
{ return _AT_Type::_S_ptr(_M_elems); }
};
......
// { dg-do compile { target c++11 } }
// { dg-options "-std=gnu++11" }
// { dg-do compile }
// Copyright (C) 2010-2016 Free Software Foundation, Inc.
//
......@@ -27,11 +28,6 @@ namespace std
template<class C> auto end(C& c) -> decltype(c.end());
template<class C> auto end(const C& c) -> decltype(c.end());
#if __cplusplus >= 201402L
template<class T, size_t N> constexpr T* begin(T (&array)[N]);
template<class T, size_t N> constexpr T* end(T (&array)[N]);
#else
template<class T, size_t N> T* begin(T (&array)[N]);
template<class T, size_t N> T* end(T (&array)[N]);
#endif
}
// { dg-options "-std=gnu++14" }
// { dg-do compile }
// Copyright (C) 2016 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/>.
#include <iterator>
namespace std
{
template<class C> auto begin(C& c) -> decltype(c.begin());
template<class C> auto begin(const C& c) -> decltype(c.begin());
template<class C> auto end(C& c) -> decltype(c.end());
template<class C> auto end(const C& c) -> decltype(c.end());
template<class T, size_t N> constexpr T* begin(T (&array)[N]);
template<class T, size_t N> constexpr T* end(T (&array)[N]);
template<class C> auto cbegin(const C& c) -> decltype(c.begin());
template<class C> auto cend(const C& c) -> decltype(c.end());
template<class C> auto rbegin(C& c) -> decltype(c.rbegin());
template<class C> auto rbegin(const C& c) -> decltype(c.rbegin());
template<class C> auto rend(C& c) -> decltype(c.rend());
template<class C> auto rend(const C& c) -> decltype(c.rend());
template<class T, size_t N>
reverse_iterator<T*> rbegin(T (&array)[N]);
template<class T, size_t N>
reverse_iterator<T*> rend(T (&array)[N]);
template<class E>
reverse_iterator<const E*> rbegin(initializer_list<E>);
template<class E>
reverse_iterator<const E*> rend(initializer_list<E>);
template<class C>
auto crbegin(const C& c) -> decltype(std::rbegin(c));
template<class C>
auto cend(const C& c) -> decltype(std::rend(c));
}
// { dg-options "-std=gnu++17" }
// { dg-do compile }
// Copyright (C) 2016 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/>.
#include <iterator>
namespace std
{
template<class C> constexpr auto begin(C& c) -> decltype(c.begin());
template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
template<class C> constexpr auto end(C& c) -> decltype(c.end());
template<class C> constexpr auto end(const C& c) -> decltype(c.end());
template<class T, size_t N> constexpr T* begin(T (&array)[N]);
template<class T, size_t N> constexpr T* end(T (&array)[N]);
template<class C> constexpr auto cbegin(const C& c) -> decltype(c.begin());
template<class C> constexpr auto cend(const C& c) -> decltype(c.end());
template<class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin());
template<class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin());
template<class C> constexpr auto rend(C& c) -> decltype(c.rend());
template<class C> constexpr auto rend(const C& c) -> decltype(c.rend());
template<class T, size_t N>
constexpr reverse_iterator<T*> rbegin(T (&array)[N]);
template<class T, size_t N>
constexpr reverse_iterator<T*> rend(T (&array)[N]);
template<class E>
constexpr reverse_iterator<const E*> rbegin(initializer_list<E>);
template<class E>
constexpr reverse_iterator<const E*> rend(initializer_list<E>);
template<class C>
constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));
template<class C>
constexpr auto cend(const C& c) -> decltype(std::rend(c));
}
......@@ -25,7 +25,7 @@
#include <vector>
// { dg-error "multiple inlined namespaces" "" { target *-*-* } 324 }
// { dg-error "multiple inlined namespaces" "" { target *-*-* } 332 }
// "template argument 1 is invalid"
// { dg-prune-output "tuple:993" }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment