Implement LWG Issue: 2280. begin/end for arrays should be constexpr and noexcept, plus a drive-by fix for cbegin/cend suggested by Peter Sommerlad.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@201703 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
9d9463a355
commit
9dacb2f713
@ -1386,7 +1386,7 @@ operator+(typename __wrap_iter<_Iter>::difference_type __n,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp, size_t _Np>
|
template <class _Tp, size_t _Np>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
_Tp*
|
_Tp*
|
||||||
begin(_Tp (&__array)[_Np])
|
begin(_Tp (&__array)[_Np])
|
||||||
{
|
{
|
||||||
@ -1394,7 +1394,7 @@ begin(_Tp (&__array)[_Np])
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp, size_t _Np>
|
template <class _Tp, size_t _Np>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
_Tp*
|
_Tp*
|
||||||
end(_Tp (&__array)[_Np])
|
end(_Tp (&__array)[_Np])
|
||||||
{
|
{
|
||||||
@ -1466,17 +1466,17 @@ reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class _Cp>
|
template <class _Cp>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
auto cbegin(const _Cp& __c) -> decltype(begin(__c))
|
auto cbegin(const _Cp& __c) -> decltype(begin(__c))
|
||||||
{
|
{
|
||||||
return _VSTD::begin(__c);
|
return begin(__c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Cp>
|
template <class _Cp>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
auto cend(const _Cp& __c) -> decltype(end(__c))
|
auto cend(const _Cp& __c) -> decltype(end(__c))
|
||||||
{
|
{
|
||||||
return _VSTD::end(__c);
|
return end(__c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Cp>
|
template <class _Cp>
|
||||||
|
@ -104,6 +104,20 @@ void test_container( std::initializer_list<T> & c, T val ) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t Sz>
|
||||||
|
void test_const_array( const T (&array)[Sz] ) {
|
||||||
|
assert ( std::begin(array) == array );
|
||||||
|
assert (*std::begin(array) == array[0] );
|
||||||
|
assert ( std::begin(array) != std::end(array));
|
||||||
|
assert ( std::end(array) == array + Sz);
|
||||||
|
#if _LIBCPP_STD_VER > 11
|
||||||
|
assert ( std::cbegin(array) == array );
|
||||||
|
assert (*std::cbegin(array) == array[0] );
|
||||||
|
assert ( std::cbegin(array) != std::cend(array));
|
||||||
|
assert ( std::cend(array) == array + Sz);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
std::vector<int> v; v.push_back(1);
|
std::vector<int> v; v.push_back(1);
|
||||||
std::list<int> l; l.push_back(2);
|
std::list<int> l; l.push_back(2);
|
||||||
@ -119,6 +133,14 @@ int main(){
|
|||||||
test_const_container ( l, 2 );
|
test_const_container ( l, 2 );
|
||||||
test_const_container ( a, 3 );
|
test_const_container ( a, 3 );
|
||||||
test_const_container ( il, 4 );
|
test_const_container ( il, 4 );
|
||||||
|
|
||||||
|
static constexpr int arrA [] { 1, 2, 3 };
|
||||||
|
test_const_array ( arrA );
|
||||||
|
#if _LIBCPP_STD_VER > 11
|
||||||
|
constexpr const int *b = std::cbegin(arrA);
|
||||||
|
constexpr const int *e = std::cend(arrA);
|
||||||
|
static_assert(e - b == 3, "");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user