From: Jonathan Wakely Date: Mon, 17 Oct 2016 17:03:09 +0000 (+0100) Subject: Backport fixes to std::experimental::sample X-Git-Tag: releases/gcc-5.5.0~764 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d73e8794f69c28329738adc7c8c8e1172fdabdf2;p=thirdparty%2Fgcc.git Backport fixes to std::experimental::sample PR libstdc++/77994 * include/experimental/algorithm (experimental::sample): Convert size argument to iterator difference type. Fix invalid use of input iterator. Defend against overloaded comma operator. From-SVN: r241263 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 4cc7444dfec0..895192159816 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2016-10-17 Jonathan Wakely + + PR libstdc++/77994 + * include/experimental/algorithm (experimental::sample): Convert size + argument to iterator difference type. Fix invalid use of input + iterator. Defend against overloaded comma operator. + 2016-10-05 Jonathan Wakely PR libstdc++/70564 diff --git a/libstdc++-v3/include/experimental/algorithm b/libstdc++-v3/include/experimental/algorithm index ce0a1491434c..11498fa8efcd 100644 --- a/libstdc++-v3/include/experimental/algorithm +++ b/libstdc++-v3/include/experimental/algorithm @@ -68,9 +68,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __distrib_type __d{}; _Size __sample_sz = 0; while (__first != __last && __sample_sz != __n) - __out[__sample_sz++] = *__first++; + { + __out[__sample_sz++] = *__first; + ++__first; + } for (auto __pop_sz = __sample_sz; __first != __last; - ++__first, ++__pop_sz) + ++__first, (void)++__pop_sz) { const auto __k = __d(__g, __param_type{0, __pop_sz}); if (__k < __n) @@ -123,9 +126,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_integral<_Distance>::value, "sample size must be an integer type"); + typename iterator_traits<_PopulationIterator>::difference_type __d = __n; return std::experimental::__sample( __first, __last, __pop_cat{}, __out, __samp_cat{}, - __n, std::forward<_UniformRandomNumberGenerator>(__g)); + __d, std::forward<_UniformRandomNumberGenerator>(__g)); } _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/testsuite/experimental/algorithm/sample.cc b/libstdc++-v3/testsuite/experimental/algorithm/sample.cc index bfff9371d7f3..8bdd5f0a1170 100644 --- a/libstdc++-v3/testsuite/experimental/algorithm/sample.cc +++ b/libstdc++-v3/testsuite/experimental/algorithm/sample.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include std::mt19937 rng;