[libcxx-commits] [libcxx] e2cfdf7 - [libc++] Fix vector<const T> (#80711)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Feb 5 15:51:36 PST 2024
Author: Nikolas Klauser
Date: 2024-02-05T15:51:32-08:00
New Revision: e2cfdf7b6a09a2159a2ce3cf4fff022b6d98b928
URL: https://github.com/llvm/llvm-project/commit/e2cfdf7b6a09a2159a2ce3cf4fff022b6d98b928
DIFF: https://github.com/llvm/llvm-project/commit/e2cfdf7b6a09a2159a2ce3cf4fff022b6d98b928.diff
LOG: [libc++] Fix vector<const T> (#80711)
#80558 introduced code that assumed that the element type of `vector` is
never const. This fixes it and adds a test. Eventually we should remove
the `allocator<const T>` extension.
Added:
libcxx/test/libcxx/containers/sequences/vector/const_T.compile.pass.cpp
Modified:
libcxx/include/__memory/uninitialized_algorithms.h
Removed:
################################################################################
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);
+}
More information about the libcxx-commits
mailing list