diff options
| author | Eric Fiselier <eric@efcs.ca> | 2015-02-10 16:46:42 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2015-02-10 16:46:42 +0000 |
| commit | 51544023a955929cbb3d105421b2e59f6be43964 (patch) | |
| tree | ef3ed22c8d0e24018181afc2b0470d5ce63b3e3b /libcxx/include/algorithm | |
| parent | 416886793f68a088dd5903caff5f720dd307bfdb (diff) | |
| download | bcm5719-llvm-51544023a955929cbb3d105421b2e59f6be43964.tar.gz bcm5719-llvm-51544023a955929cbb3d105421b2e59f6be43964.zip | |
[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
llvm-svn: 228704
Diffstat (limited to 'libcxx/include/algorithm')
| -rw-r--r-- | libcxx/include/algorithm | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 68173f472c8..475ba66ad38 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -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; |

