[libcxx] Properly convert the count arguments to the *_n algorithms before use.
Summary: The requirement on the `Size` type passed to *_n algorithms is that it is convertible to an integral type. This means we can't use a variable of type `Size` directly. Instead we need to convert it to an integral type first. The problem is finding out what integral type to convert it to. `__convert_to_integral` figures out what integral type to convert it to and performs the conversion, It also promotes the resulting integral type so that it is at least as big as an integer. `__convert_to_integral` also has a special case for converting enums. This should only work on non-scoped enumerations because it does not apply an explicit conversion from the enum to its underlying type. Reviewers: chandlerc, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D7449 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@228704 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -1672,7 +1672,8 @@ search_n(_ForwardIterator __first, _ForwardIterator __last,
|
||||
_Size __count, const _Tp& __value_, _BinaryPredicate __pred)
|
||||
{
|
||||
return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
|
||||
(__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
|
||||
(__first, __last, __convert_to_integral(__count), __value_, __pred,
|
||||
typename iterator_traits<_ForwardIterator>::iterator_category());
|
||||
}
|
||||
|
||||
template <class _ForwardIterator, class _Size, class _Tp>
|
||||
@@ -1681,7 +1682,8 @@ _ForwardIterator
|
||||
search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
|
||||
{
|
||||
typedef typename iterator_traits<_ForwardIterator>::value_type __v;
|
||||
return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
|
||||
return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
|
||||
__value_, __equal_to<__v, _Tp>());
|
||||
}
|
||||
|
||||
// copy
|
||||
@@ -1839,8 +1841,10 @@ typename enable_if
|
||||
!__is_random_access_iterator<_InputIterator>::value,
|
||||
_OutputIterator
|
||||
>::type
|
||||
copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
|
||||
copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
|
||||
{
|
||||
typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
|
||||
_IntegralSize __n = __orig_n;
|
||||
if (__n > 0)
|
||||
{
|
||||
*__result = *__first;
|
||||
@@ -1862,8 +1866,10 @@ typename enable_if
|
||||
__is_random_access_iterator<_InputIterator>::value,
|
||||
_OutputIterator
|
||||
>::type
|
||||
copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
|
||||
copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
|
||||
{
|
||||
typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
|
||||
_IntegralSize __n = __orig_n;
|
||||
return _VSTD::copy(__first, __first + __n, __result);
|
||||
}
|
||||
|
||||
@@ -2055,7 +2061,7 @@ inline _LIBCPP_INLINE_VISIBILITY
|
||||
_OutputIterator
|
||||
fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
|
||||
{
|
||||
return _VSTD::__fill_n(__first, __n, __value_);
|
||||
return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
|
||||
}
|
||||
|
||||
// fill
|
||||
@@ -2101,8 +2107,10 @@ generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
|
||||
template <class _OutputIterator, class _Size, class _Generator>
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
_OutputIterator
|
||||
generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
|
||||
generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
|
||||
{
|
||||
typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
|
||||
_IntegralSize __n = __orig_n;
|
||||
for (; __n > 0; ++__first, (void) --__n)
|
||||
*__first = __gen();
|
||||
return __first;
|
||||
|
Reference in New Issue
Block a user