[libcxx-commits] [libcxx] [libc++] Implement part of P2562R1: constexpr `std::inplace_merge` (PR #129008)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 5 01:36:28 PST 2025


================
@@ -25,99 +23,146 @@
 
 #if TEST_STD_VER >= 11
 struct S {
-    S() : i_(0) {}
-    S(int i) : i_(i) {}
-
-    S(const S&  rhs) : i_(rhs.i_) {}
-    S(      S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; }
-
-    S& operator =(const S&  rhs) { i_ = rhs.i_;              return *this; }
-    S& operator =(      S&& rhs) { i_ = rhs.i_; rhs.i_ = -2; assert(this != &rhs); return *this; }
-    S& operator =(int i)         { i_ = i;                   return *this; }
-
-    bool operator  <(const S&  rhs) const { return i_ < rhs.i_; }
-    bool operator ==(const S&  rhs) const { return i_ == rhs.i_; }
-    bool operator ==(int i)         const { return i_ == i; }
-
-    void set(int i) { i_ = i; }
-
-    int i_;
-    };
+  TEST_CONSTEXPR_CXX26 S() : i_(0) {}
+  TEST_CONSTEXPR_CXX26 S(int i) : i_(i) {}
+
+  TEST_CONSTEXPR_CXX26 S(const S& rhs) : i_(rhs.i_) {}
+  TEST_CONSTEXPR_CXX26 S(S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; }
+
+  TEST_CONSTEXPR_CXX26 S& operator=(const S& rhs) {
+    i_ = rhs.i_;
+    return *this;
+  }
+  TEST_CONSTEXPR_CXX26 S& operator=(S&& rhs) {
+    i_     = rhs.i_;
+    rhs.i_ = -2;
+    assert(this != &rhs);
+    return *this;
+  }
+  TEST_CONSTEXPR_CXX26 S& operator=(int i) {
+    i_ = i;
+    return *this;
+  }
+
+  TEST_CONSTEXPR_CXX26 bool operator<(const S& rhs) const { return i_ < rhs.i_; }
+  TEST_CONSTEXPR_CXX26 bool operator==(const S& rhs) const { return i_ == rhs.i_; }
+  TEST_CONSTEXPR_CXX26 bool operator==(int i) const { return i_ == i; }
+
+  TEST_CONSTEXPR_CXX26 void set(int i) { i_ = i; }
+
+  int i_;
+};
 #endif
 
 std::mt19937 randomness;
 
 template <class Iter>
-void
-test_one(unsigned N, unsigned M)
-{
-    typedef typename std::iterator_traits<Iter>::value_type value_type;
-    assert(M <= N);
-    value_type* ia = new value_type[N];
-    for (unsigned i = 0; i < N; ++i)
-        ia[i] = i;
-    std::shuffle(ia, ia+N, randomness);
-    std::sort(ia, ia+M);
-    std::sort(ia+M, ia+N);
-    std::inplace_merge(Iter(ia), Iter(ia+M), Iter(ia+N));
-    if(N > 0)
-    {
-        assert(ia[0] == 0);
-        assert(ia[N-1] == static_cast<value_type>(N-1));
-        assert(std::is_sorted(ia, ia+N));
-    }
-    delete [] ia;
+void test_one_randomized(unsigned N, unsigned M) {
+  typedef typename std::iterator_traits<Iter>::value_type value_type;
+  value_type* ia = new value_type[N];
+  for (unsigned i = 0; i < N; ++i)
+    ia[i] = i;
+  std::shuffle(ia, ia + N, randomness);
+  std::sort(ia, ia + M);
+  std::sort(ia + M, ia + N);
+  std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N));
+  if (N > 0) {
+    assert(ia[0] == 0);
+    assert(ia[N - 1] == static_cast<value_type>(N - 1));
+    assert(std::is_sorted(ia, ia + N));
+  }
+  delete[] ia;
+}
+
+template <class Iter>
+TEST_CONSTEXPR_CXX26 void test_one_non_randomized(unsigned N, unsigned M) {
----------------
frederick-vs-ja wrote:

Actually, I think it would make more sense to only keep the old function while making it constant-evaluation-compatible. Perhaps we can create `constexpr` versions for `std::mt19937` and `std::shuffle` and use them in constant evaluation.

https://github.com/llvm/llvm-project/pull/129008


More information about the libcxx-commits mailing list