[libcxx-commits] [libcxx] [libc++] Implement P3508R0: `Wording for "constexpr for specialized memory algorithms"` (PR #197313)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 14 19:09:55 PDT 2026


================
@@ -42,6 +43,55 @@ struct NotConvertibleFromInt {};
 static_assert(!std::is_invocable_v<decltype(std::ranges::uninitialized_copy), int*, int*, NotConvertibleFromInt*,
                                    NotConvertibleFromInt*>);
 
+TEST_CONSTEXPR_CXX26 bool test() {
+  constexpr int n       = 3;
+  const ConstCopy in[n] = {ConstCopy(1), ConstCopy(2), ConstCopy(3)};
+  std::allocator<ConstCopy> alloc;
+
+  // (iter, sentinel) overload.
+  {
+    ConstCopy* out = alloc.allocate(n);
+    auto result    = std::ranges::uninitialized_copy(in, in + n, out, out + n);
+    assert(result.in == in + n);
+    assert(result.out == out + n);
+    for (int i = 0; i != n; ++i)
+      assert(out[i].val == in[i].val);
+
+    std::destroy(out, out + n);
+    alloc.deallocate(out, n);
+  }
+
+  // (range) overload.
+  {
+    ConstCopy* out = alloc.allocate(n);
+    auto out_range = std::ranges::subrange(out, out + n);
+    auto result    = std::ranges::uninitialized_copy(in, out_range);
+    assert(result.in == in + n);
+    assert(result.out == out + n);
+    for (int i = 0; i != n; ++i)
+      assert(out[i].val == in[i].val);
+
+    std::destroy(out, out + n);
+    alloc.deallocate(out, n);
+  }
+
+  // Destination range is shorter than the source range.
+  {
+    constexpr int m = 2;
+    ConstCopy* out  = alloc.allocate(m);
+    auto result     = std::ranges::uninitialized_copy(in, in + n, out, out + m);
+    assert(result.in == in + m);
+    assert(result.out == out + m);
+    for (int i = 0; i != m; ++i)
+      assert(out[i].val == in[i].val);
+
+    std::destroy(out, out + m);
+    alloc.deallocate(out, m);
+  }
+
+  return true;
+}
+
 int main(int, char**) {
   // An empty range -- no default constructors should be invoked.
----------------
frederick-vs-ja wrote:

Existing test cases are somehow `constexpr`-unfriendly. IMO it would be clearer to sweep them into a separate function (e.g. `runtime_only_test`). Same for other test files. This potentially allows we to improve them.

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


More information about the libcxx-commits mailing list