From 109b7ca47ec0a0c293b17bd2be3874623d633539 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Mon, 23 Mar 2026 07:32:27 +0800 Subject: [PATCH] Workaround for MSVC's `constexpr` array allocation bug for `vector` --- stl/inc/vector | 4 ++++ stl/inc/xmemory | 9 +++++++++ tests/std/tests/P1004R2_constexpr_vector/test.cpp | 4 ---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/stl/inc/vector b/stl/inc/vector index cc54d2137ec..c0e318c287b 100644 --- a/stl/inc/vector +++ b/stl/inc/vector @@ -1656,7 +1656,11 @@ private: _Newvec = _Allocate_at_least_helper(_Al, _Newcapacity); } else { _STL_INTERNAL_STATIC_ASSERT(_Policy == _Reallocation_policy::_Exactly); +#if !defined(__clang__) && !defined(__EDG__) // TRANSITION, DevCom-11060638 + _Newvec = _Al.allocate(_Newcapacity) + decltype(pointer{} - pointer{}){0}; +#else // ^^^ workaround / no workaround vvv _Newvec = _Al.allocate(_Newcapacity); +#endif // ^^^ no workaround ^^^ } _Allocation_guard<_Alty> _Guard{_Al, _Newvec, _Newcapacity}; diff --git a/stl/inc/xmemory b/stl/inc/xmemory index 6ee39ff8e2b..769ab1d98ae 100644 --- a/stl/inc/xmemory +++ b/stl/inc/xmemory @@ -2320,11 +2320,20 @@ _NODISCARD_RAW_PTR_ALLOC _CONSTEXPR20 typename allocator_traits<_Alloc>::pointer if constexpr (_Should_allocate_at_least<_Alloc>) { auto [_Ptr, _Allocated] = _Al.allocate_at_least(_Count); _Count = _Allocated; +#if !defined(__clang__) && !defined(__EDG__) // TRANSITION, DevCom-11060638 + return _Ptr + decltype(_Ptr - _Ptr){0}; +#else // ^^^ workaround / no workaround vvv return _Ptr; +#endif // ^^^ no workaround ^^^ } else #endif // _HAS_CXX23 { +#if !defined(__clang__) && !defined(__EDG__) // TRANSITION, DevCom-11060638 + using _Pointer = decltype(_Al.allocate(_Count)); + return _Al.allocate(_Count) + decltype(_Pointer{} - _Pointer{}){0}; +#else // ^^^ workaround / no workaround vvv return _Al.allocate(_Count); +#endif // ^^^ no workaround ^^^ } } diff --git a/tests/std/tests/P1004R2_constexpr_vector/test.cpp b/tests/std/tests/P1004R2_constexpr_vector/test.cpp index b2702dbf378..c165a2b28a1 100644 --- a/tests/std/tests/P1004R2_constexpr_vector/test.cpp +++ b/tests/std/tests/P1004R2_constexpr_vector/test.cpp @@ -679,15 +679,11 @@ constexpr bool test_vector_of_array() { #endif // !_HAS_CXX23 { test_vector_of_array_impl>, 1>(); -#if defined(__clang__) || defined(__EDG__) // TRANSITION, GH-6155 (not yet reported to MSVC) test_vector_of_array_impl>, 42>(); -#endif // ^^^ no workaround ^^^ } test_vector_of_array_impl, 1>(); -#if defined(__clang__) || defined(__EDG__) // TRANSITION, GH-6155 (not yet reported to MSVC) test_vector_of_array_impl, 42>(); -#endif // ^^^ no workaround ^^^ return true; }