[libcxx-commits] [libcxx] [libc++] Fix vector<const T> (PR #80711)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 5 08:53:20 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

#<!-- -->80558 introduced code that assumed that the element type of `vector` is nenver const. This fixes it and adds a test. Eventually we should remove the `allocator<const T>` extension,


---
Full diff: https://github.com/llvm/llvm-project/pull/80711.diff


2 Files Affected:

- (modified) libcxx/include/__memory/uninitialized_algorithms.h (+1-1) 
- (added) libcxx/test/libcxx/containers/sequences/vector/const_T.compile.pass.cpp (+18) 


``````````diff
diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h
index 9733bb748f665..7e25a5c5fa19b 100644
--- a/libcxx/include/__memory/uninitialized_algorithms.h
+++ b/libcxx/include/__memory/uninitialized_algorithms.h
@@ -643,7 +643,7 @@ __uninitialized_allocator_relocate(_Alloc& __alloc, _Tp* __first, _Tp* __last, _
     __guard.__complete();
     std::__allocator_destroy(__alloc, __first, __last);
   } else {
-    __builtin_memcpy(__result, __first, sizeof(_Tp) * (__last - __first));
+    __builtin_memcpy(const_cast<__remove_const_t<_Tp>*>(__result), __first, sizeof(_Tp) * (__last - __first));
   }
 }
 
diff --git a/libcxx/test/libcxx/containers/sequences/vector/const_T.compile.pass.cpp b/libcxx/test/libcxx/containers/sequences/vector/const_T.compile.pass.cpp
new file mode 100644
index 0000000000000..62fff96ac5abe
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/vector/const_T.compile.pass.cpp
@@ -0,0 +1,18 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Make sure that `vector<const T>` works
+
+#include <vector>
+
+void test() {
+  std::vector<const int> v;
+  v.emplace_back(1);
+  v.push_back(1);
+  v.resize(3);
+}

``````````

</details>


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


More information about the libcxx-commits mailing list