[libcxx-commits] [libcxx] fa49ad5 - [libc++] Fix random_shuffle signature in C++03 mode with frozen headers (#186443)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 20 08:22:47 PDT 2026


Author: Zibi Sarbinowski
Date: 2026-03-20T11:22:42-04:00
New Revision: fa49ad564bae659f9e189dbc4b2b56fd21a2fe33

URL: https://github.com/llvm/llvm-project/commit/fa49ad564bae659f9e189dbc4b2b56fd21a2fe33
DIFF: https://github.com/llvm/llvm-project/commit/fa49ad564bae659f9e189dbc4b2b56fd21a2fe33.diff

LOG: [libc++] Fix random_shuffle signature in C++03 mode with frozen headers (#186443)

The frozen C++03 headers got an invalid simplification in #134045 that
changed the signature of random_shuffle to use a forwarding reference
instead of a lvalue reference. This patch fixes it and adds a test.

---------

Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>

Added: 
    

Modified: 
    libcxx/include/__cxx03/__algorithm/shuffle.h
    libcxx/test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__cxx03/__algorithm/shuffle.h b/libcxx/include/__cxx03/__algorithm/shuffle.h
index fee7028ae22ac..24cd71c71378b 100644
--- a/libcxx/include/__cxx03/__algorithm/shuffle.h
+++ b/libcxx/include/__cxx03/__algorithm/shuffle.h
@@ -109,7 +109,7 @@ _LIBCPP_HIDE_FROM_ABI void random_shuffle(_RandomAccessIterator __first, _Random
 
 template <class _RandomAccessIterator, class _RandomNumberGenerator>
 _LIBCPP_HIDE_FROM_ABI void
-random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, _RandomNumberGenerator&& __rand) {
+random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, _RandomNumberGenerator& __rand) {
   typedef typename iterator_traits<_RandomAccessIterator>::
diff erence_type 
diff erence_type;
   
diff erence_type __d = __last - __first;
   if (__d > 1) {

diff  --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
index 5a2b58f4a5354..620561511a5a5 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.random.shuffle/random_shuffle_rand.pass.cpp
@@ -8,11 +8,11 @@
 
 // <algorithm>
 
-// template<RandomAccessIterator Iter, Callable<auto, Iter::
diff erence_type> Rand>
-//   requires ShuffleIterator<Iter>
-//         && Convertible<Rand::result_type, Iter::
diff erence_type>
-//   void
-//   random_shuffle(Iter first, Iter last, Rand&& rand);
+// template<class RandomAccessIterator, class RandomFunc>
+//   void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomFunc& rand); // until C++11
+//
+// template<class RandomAccessIterator, class RandomFunc>
+//   void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomFunc&& rand); // since C++11
 
 // REQUIRES: c++03 || c++11 || c++14
 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
@@ -20,42 +20,43 @@
 #include <algorithm>
 #include <cassert>
 #include <cstddef>
+#include <iterator>
 
 #include "test_macros.h"
 #include "test_iterators.h"
 
-
-struct gen
-{
-    std::ptr
diff _t operator()(std::ptr
diff _t n)
-    {
-        return n-1;
-    }
+struct RandomGenerator {
+  std::ptr
diff _t operator()(std::ptr
diff _t n) { return n - 1; }
 };
 
-
 template <class Iter>
-void
-test_with_iterator()
-{
-
-    int ia[] = {1, 2, 3, 4};
-    int ia1[] = {4, 1, 2, 3};
-    const unsigned sa = sizeof(ia)/sizeof(ia[0]);
-    gen r;
-
-    std::random_shuffle(ia, ia+sa, r);
-    LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
-    assert(std::is_permutation(ia, ia+sa, ia1));
-
-    std::random_shuffle(ia, ia+sa, r);
-    assert(std::is_permutation(ia, ia+sa, ia1));
+void test_with_iterator() {
+  RandomGenerator gen;
+  int values[] = {1, 2, 3, 4};
+
+  // Make sure the algorithm shuffles
+  {
+    int arr[] = {1, 2, 3, 4};
+    std::random_shuffle(Iter(std::begin(arr)), Iter(std::end(arr)), gen);
+    int libcxx_expected[] = {4, 1, 2, 3};
+    LIBCPP_ASSERT(std::equal(std::begin(arr), std::end(arr), std::begin(libcxx_expected)));
+    assert(std::is_permutation(std::begin(arr), std::end(arr), std::begin(values)));
+    std::random_shuffle(Iter(std::begin(arr)), Iter(std::end(arr)), gen);
+    assert(std::is_permutation(std::begin(arr), std::end(arr), std::begin(values)));
+  }
+
+  // Test the signature in C++03 mode, which takes a lvalue reference
+#if TEST_STD_VER == 03
+  {
+    int arr[] = {1, 2, 3, 4};
+    std::random_shuffle<Iter, RandomGenerator>(Iter(std::begin(arr)), Iter(std::end(arr)), gen);
+    assert(std::is_permutation(std::begin(arr), std::end(arr), std::begin(values)));
+  }
+#endif
 }
 
-
-int main(int, char**)
-{
-    test_with_iterator<random_access_iterator<int*> >();
-    test_with_iterator<int*>();
-    return 0;
+int main(int, char**) {
+  test_with_iterator<random_access_iterator<int*> >();
+  test_with_iterator<int*>();
+  return 0;
 }


        


More information about the libcxx-commits mailing list