diff --git a/include/iterator b/include/iterator index 979a9645..d16aa2aa 100644 --- a/include/iterator +++ b/include/iterator @@ -309,6 +309,19 @@ template auto end(const C& c) -> decltype(c.end()); template T* begin(T (&array)[N]); template T* end(T (&array)[N]); +template auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14 +template auto cend(const C& c) -> decltype(std::end(c)); // C++14 +template auto rbegin(C& c) -> decltype(c.rbegin()); // C++14 +template auto rbegin(const C& c) -> decltype(c.rbegin()); // C++14 +template auto rend(C& c) -> decltype(c.rend()); // C++14 +template auto rend(const C& c) -> decltype(c.rend()); // C++14 +template reverse_iterator rbegin(initializer_list il); // C++14 +template reverse_iterator rend(initializer_list il); // C++14 +template reverse_iterator rbegin(T (&array)[N]); // C++14 +template reverse_iterator rend(T (&array)[N]); // C++14 +template auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14 +template auto crend(const C& c) -> decltype(std::rend(c)); // C++14 + } // std */ @@ -317,6 +330,7 @@ template T* end(T (&array)[N]); #include #include #include +#include #ifdef __APPLE__ #include #endif @@ -1405,6 +1419,67 @@ end(const _Cp& __c) -> decltype(__c.end()) return __c.end(); } +#if _LIBCPP_STD_VER > 11 + +template +inline _LIBCPP_INLINE_VISIBILITY +auto cbegin(const _Cp& __c) -> decltype(begin(__c)) +{ + return __c.begin(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto cend(const _Cp& __c) -> decltype(end(__c)) +{ + return __c.end(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto rbegin(_Cp& __c) -> decltype(__c.rbegin()) +{ + return __c.rbegin(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto rbegin(const _Cp& __c) -> decltype(__c.rbegin()) +{ + return __c.rbegin(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto rend(_Cp& __c) -> decltype(__c.rend()) +{ + return __c.rend(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto rend(const _Cp& __c) -> decltype(__c.rend()) +{ + return __c.rend(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto crbegin(const _Cp& __c) -> decltype(rbegin(__c)) +{ + return rbegin(__c); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +auto crend(const _Cp& __c) -> decltype(rend(__c)) +{ + return rend(__c); +} + +#endif + + #else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN) template @@ -1457,6 +1532,37 @@ end(_Tp (&__array)[_Np]) return __array + _Np; } +#if _LIBCPP_STD_VER > 11 +template +inline _LIBCPP_INLINE_VISIBILITY +reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np]) +{ + return reverse_iterator<_Tp*>(__array + _Np); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np]) +{ + return reverse_iterator<_Tp*>(__array); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +reverse_iterator rbegin(initializer_list<_Ep> __il) +{ + return reverse_iterator(__il.end()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +reverse_iterator rend(initializer_list<_Ep> __il) +{ + return reverse_iterator(__il.begin()); +} + +#endif + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_ITERATOR diff --git a/test/iterators/iterator.range/begin-end.pass.cpp b/test/iterators/iterator.range/begin-end.pass.cpp new file mode 100644 index 00000000..e8a6c18f --- /dev/null +++ b/test/iterators/iterator.range/begin-end.pass.cpp @@ -0,0 +1,125 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// template auto begin(C& c) -> decltype(c.begin()); +// template auto begin(const C& c) -> decltype(c.begin()); +// template auto end(C& c) -> decltype(c.end()); +// template auto end(const C& c) -> decltype(c.end()); +// template reverse_iterator rbegin(initializer_list il); +// template reverse_iterator rend(initializer_list il); + +#include <__config> + +#if __cplusplus >= 201103L +#include +#include +#include +#include +#include +#include + +template +void test_const_container( const C & c, typename C::value_type val ) { + assert ( std::begin(c) == c.begin()); + assert (*std::begin(c) == val ); + assert ( std::begin(c) != c.end()); + assert ( std::end(c) == c.end()); +#if _LIBCPP_STD_VER > 11 + assert ( std::cbegin(c) == c.cbegin()); + assert ( std::cbegin(c) != c.cend()); + assert ( std::cend(c) == c.cend()); + assert ( std::rbegin(c) == c.rbegin()); + assert ( std::rbegin(c) != c.rend()); + assert ( std::rend(c) == c.rend()); + assert ( std::crbegin(c) == c.crbegin()); + assert ( std::crbegin(c) != c.crend()); + assert ( std::crend(c) == c.crend()); +#endif + } + +template +void test_const_container( const std::initializer_list & c, T val ) { + assert ( std::begin(c) == c.begin()); + assert (*std::begin(c) == val ); + assert ( std::begin(c) != c.end()); + assert ( std::end(c) == c.end()); +#if _LIBCPP_STD_VER > 11 +// initializer_list doesn't have cbegin/cend/rbegin/rend +// assert ( std::cbegin(c) == c.cbegin()); +// assert ( std::cbegin(c) != c.cend()); +// assert ( std::cend(c) == c.cend()); +// assert ( std::rbegin(c) == c.rbegin()); +// assert ( std::rbegin(c) != c.rend()); +// assert ( std::rend(c) == c.rend()); +// assert ( std::crbegin(c) == c.crbegin()); +// assert ( std::crbegin(c) != c.crend()); +// assert ( std::crend(c) == c.crend()); +#endif + } + +template +void test_container( C & c, typename C::value_type val ) { + assert ( std::begin(c) == c.begin()); + assert (*std::begin(c) == val ); + assert ( std::begin(c) != c.end()); + assert ( std::end(c) == c.end()); +#if _LIBCPP_STD_VER > 11 + assert ( std::cbegin(c) == c.cbegin()); + assert ( std::cbegin(c) != c.cend()); + assert ( std::cend(c) == c.cend()); + assert ( std::rbegin(c) == c.rbegin()); + assert ( std::rbegin(c) != c.rend()); + assert ( std::rend(c) == c.rend()); + assert ( std::crbegin(c) == c.crbegin()); + assert ( std::crbegin(c) != c.crend()); + assert ( std::crend(c) == c.crend()); +#endif + } + +template +void test_container( std::initializer_list & c, T val ) { + assert ( std::begin(c) == c.begin()); + assert (*std::begin(c) == val ); + assert ( std::begin(c) != c.end()); + assert ( std::end(c) == c.end()); +#if _LIBCPP_STD_VER > 11 +// initializer_list doesn't have cbegin/cend/rbegin/rend +// assert ( std::cbegin(c) == c.cbegin()); +// assert ( std::cbegin(c) != c.cend()); +// assert ( std::cend(c) == c.cend()); +// assert ( std::rbegin(c) == c.rbegin()); +// assert ( std::rbegin(c) != c.rend()); +// assert ( std::rend(c) == c.rend()); +// assert ( std::crbegin(c) == c.crbegin()); +// assert ( std::crbegin(c) != c.crend()); +// assert ( std::crend(c) == c.crend()); +#endif + } + +int main(){ + std::vector v; v.push_back(1); + std::list l; l.push_back(2); + std::array a; a[0] = 3; + std::initializer_list il = { 4 }; + + test_container ( v, 1 ); + test_container ( l, 2 ); + test_container ( a, 3 ); + test_container ( il, 4 ); + + test_const_container ( v, 1 ); + test_const_container ( l, 2 ); + test_const_container ( a, 3 ); + test_const_container ( il, 4 ); +} + +#else +int main(){} +#endif diff --git a/www/cxx1y_status.html b/www/cxx1y_status.html index 0a3047c7..6bf68e1f 100644 --- a/www/cxx1y_status.html +++ b/www/cxx1y_status.html @@ -142,7 +142,7 @@ 2093Throws clause of condition_variable::wait with predicateBristolComplete 2094duration conversion overflow shouldn't participate in overload resolutionBristol 2122merge() stability for lists versus forward listsBristol - 2128Absence of global functions cbegin/cendBristol + 2128Absence of global functions cbegin/cendBristolComplete 2145error_category default constructorBristolComplete 2147Unclear hint type in Allocator's allocate functionBristolComplete 2148Hashing enums should be supported directly by std::hashBristol