]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Backport fixes to std::experimental::sample
authorJonathan Wakely <jwakely@redhat.com>
Mon, 17 Oct 2016 17:03:09 +0000 (18:03 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 17 Oct 2016 17:03:09 +0000 (18:03 +0100)
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

libstdc++-v3/ChangeLog
libstdc++-v3/include/experimental/algorithm
libstdc++-v3/testsuite/experimental/algorithm/sample.cc

index 4cc7444dfec0d44439197f2a26abfb4ed3a260da..89519215981666394b4b353646467548b65e2b9e 100644 (file)
@@ -1,3 +1,10 @@
+2016-10-17  Jonathan Wakely  <jwakely@redhat.com>
+
+       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  <jwakely@redhat.com>
 
        PR libstdc++/70564
index ce0a1491434c26cffa8c4c9f424e1a17f1763158..11498fa8efcdb7ed2d47c12e2796d46744b63aae 100644 (file)
@@ -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
index bfff9371d7f3d77f522a862ab0ff2a092a61f7ac..8bdd5f0a11705cf0b8b3813fd8745b5326c8734a 100644 (file)
@@ -22,6 +22,7 @@
 #include <sstream>
 #include <forward_list>
 #include <vector>
+#include <random>
 #include <testsuite_hooks.h>
 
 std::mt19937 rng;