LWG Issue 2128: Implement global cbegin/rbegin/cend/rbegin

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@189634 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2013-08-30 01:17:07 +00:00
parent 161a9bf447
commit 09da3c053c
3 changed files with 232 additions and 1 deletions

View File

@ -309,6 +309,19 @@ template <class C> auto end(const C& c) -> decltype(c.end());
template <class T, size_t N> T* begin(T (&array)[N]);
template <class T, size_t N> T* end(T (&array)[N]);
template <class C> auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14
template <class C> auto cend(const C& c) -> decltype(std::end(c)); // C++14
template <class C> auto rbegin(C& c) -> decltype(c.rbegin()); // C++14
template <class C> auto rbegin(const C& c) -> decltype(c.rbegin()); // C++14
template <class C> auto rend(C& c) -> decltype(c.rend()); // C++14
template <class C> auto rend(const C& c) -> decltype(c.rend()); // C++14
template <class E> reverse_iterator<const E*> rbegin(initializer_list<E> il); // C++14
template <class E> reverse_iterator<const E*> rend(initializer_list<E> il); // C++14
template <class T, size_t N> reverse_iterator<T*> rbegin(T (&array)[N]); // C++14
template <class T, size_t N> reverse_iterator<T*> rend(T (&array)[N]); // C++14
template <class C> auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
template <class C> auto crend(const C& c) -> decltype(std::rend(c)); // C++14
} // std
*/
@ -317,6 +330,7 @@ template <class T, size_t N> T* end(T (&array)[N]);
#include <type_traits>
#include <cstddef>
#include <iosfwd>
#include <initializer_list>
#ifdef __APPLE__
#include <Availability.h>
#endif
@ -1405,6 +1419,67 @@ end(const _Cp& __c) -> decltype(__c.end())
return __c.end();
}
#if _LIBCPP_STD_VER > 11
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto cbegin(const _Cp& __c) -> decltype(begin(__c))
{
return __c.begin();
}
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto cend(const _Cp& __c) -> decltype(end(__c))
{
return __c.end();
}
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
{
return __c.rbegin();
}
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
{
return __c.rbegin();
}
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto rend(_Cp& __c) -> decltype(__c.rend())
{
return __c.rend();
}
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto rend(const _Cp& __c) -> decltype(__c.rend())
{
return __c.rend();
}
template <class _Cp>
inline _LIBCPP_INLINE_VISIBILITY
auto crbegin(const _Cp& __c) -> decltype(rbegin(__c))
{
return rbegin(__c);
}
template <class _Cp>
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 <class _Cp>
@ -1457,6 +1532,37 @@ end(_Tp (&__array)[_Np])
return __array + _Np;
}
#if _LIBCPP_STD_VER > 11
template <class _Tp, size_t _Np>
inline _LIBCPP_INLINE_VISIBILITY
reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
{
return reverse_iterator<_Tp*>(__array + _Np);
}
template <class _Tp, size_t _Np>
inline _LIBCPP_INLINE_VISIBILITY
reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
{
return reverse_iterator<_Tp*>(__array);
}
template <class _Ep>
inline _LIBCPP_INLINE_VISIBILITY
reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
{
return reverse_iterator<const _Ep*>(__il.end());
}
template <class _Ep>
inline _LIBCPP_INLINE_VISIBILITY
reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
{
return reverse_iterator<const _Ep*>(__il.begin());
}
#endif
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_ITERATOR

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <iterator>
// 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 E> reverse_iterator<const E*> rbegin(initializer_list<E> il);
// template <class E> reverse_iterator<const E*> rend(initializer_list<E> il);
#include <__config>
#if __cplusplus >= 201103L
#include <iterator>
#include <cassert>
#include <vector>
#include <array>
#include <list>
#include <initializer_list>
template<typename C>
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<typename T>
void test_const_container( const std::initializer_list<T> & 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<typename C>
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<typename T>
void test_container( std::initializer_list<T> & 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<int> v; v.push_back(1);
std::list<int> l; l.push_back(2);
std::array<int, 1> a; a[0] = 3;
std::initializer_list<int> 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

View File

@ -142,7 +142,7 @@
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2093">2093</a></td><td>Throws clause of condition_variable::wait with predicate</td><td>Bristol</td><td>Complete</td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2094">2094</a></td><td>duration conversion overflow shouldn't participate in overload resolution</td><td>Bristol</td><td></td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2122">2122</a></td><td>merge() stability for lists versus forward lists</td><td>Bristol</td><td></td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2128">2128</a></td><td>Absence of global functions cbegin/cend</td><td>Bristol</td><td></td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2128">2128</a></td><td>Absence of global functions cbegin/cend</td><td>Bristol</td><td>Complete</td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2145">2145</a></td><td>error_category default constructor</td><td>Bristol</td><td>Complete</td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2147">2147</a></td><td>Unclear hint type in Allocator's allocate function</td><td>Bristol</td><td>Complete</td></tr>
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148">2148</a></td><td>Hashing enums should be supported directly by std::hash</td><td>Bristol</td><td></td></tr>