Skip to content

Commit 7cbf959

Browse files
[libc++][test] Avoid using allocator<const T> (#73545)
Found while running libc++'s test suite with MSVC's STL. MSVC's STL rejects `allocator<const T>`. This may or may not be justified by the current Standardese (it was bogus in the C++03 era), but it's how we reject usage like `vector<const T>`. A bunch of `mdspan` tests are failing for us because some centralized machinery is using `allocator<const T>`. Testing that `mdspan` and its associated types work properly with `const T` is good and necessary, but directly allocating `const T` is what's a problem for MSVC's STL. I'd like to ask for a very targeted change here that preserves all of the test coverage but changes how `ElementPool` interacts with `allocator`. This intentionally leaves `ElementPool::get_ptr()` returning `T*` (pointer-to-possibly-const), so there's no externally visible difference.
1 parent 67268da commit 7cbf959

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

libcxx/test/std/containers/views/mdspan/MinimalElementType.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#ifndef TEST_STD_CONTAINERS_VIEWS_MDSPAN_MINIMAL_ELEMENT_TYPE_H
1010
#define TEST_STD_CONTAINERS_VIEWS_MDSPAN_MINIMAL_ELEMENT_TYPE_H
1111

12-
#include<memory>
12+
#include <memory>
13+
#include <type_traits>
1314

1415
// Idiosyncratic element type for mdspan
1516
// Make sure we don't assume copyable, default constructible, movable etc.
@@ -25,7 +26,7 @@ struct MinimalElementType {
2526
template<class T, size_t N>
2627
struct ElementPool {
2728
constexpr ElementPool() {
28-
ptr_ = std::allocator<T>().allocate(N);
29+
ptr_ = std::allocator<std::remove_const_t<T>>().allocate(N);
2930
for (int i = 0; i != N; ++i)
3031
std::construct_at(ptr_ + i, 42);
3132
}
@@ -35,11 +36,11 @@ struct ElementPool {
3536
constexpr ~ElementPool() {
3637
for (int i = 0; i != N; ++i)
3738
std::destroy_at(ptr_ + i);
38-
std::allocator<T>().deallocate(ptr_, N);
39+
std::allocator<std::remove_const_t<T>>().deallocate(ptr_, N);
3940
}
4041

4142
private:
42-
T* ptr_;
43+
std::remove_const_t<T>* ptr_;
4344
};
4445

4546
#endif // TEST_STD_CONTAINERS_VIEWS_MDSPAN_MINIMAL_ELEMENT_TYPE_H

0 commit comments

Comments
 (0)