From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id BEFD73888C4E; Thu, 21 Apr 2022 12:33:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BEFD73888C4E MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-9908] libstdc++: Ignore cv-quals when std::allocator constructs X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 97f9d2f9b2a6172f31c971ac0d99ebc712b2501b X-Git-Newrev: 7bb5e5768b79594266468f71ee61c3dfd2d3c3bd Message-Id: <20220421123308.BEFD73888C4E@sourceware.org> Date: Thu, 21 Apr 2022 12:33:08 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Apr 2022 12:33:08 -0000 https://gcc.gnu.org/g:7bb5e5768b79594266468f71ee61c3dfd2d3c3bd commit r11-9908-g7bb5e5768b79594266468f71ee61c3dfd2d3c3bd Author: Jonathan Wakely Date: Thu Jan 13 21:59:13 2022 +0000 libstdc++: Ignore cv-quals when std::allocator constructs When I added the std::allocator_traits> specialization it broke code like this: std::allocate_shared(std::allocator()); The problem is that allocator_traits>::construct(a, p) now uses std::_Construct(p), which only does a static_cast(p) and so fails if the pointer has cv-quals. This changes std::_Construct (and the related std::_Construct_novalue) to use a C-style cast to (void*) which matches the effects of the "voidify" helper in the C++20 standard. libstdc++-v3/ChangeLog: * include/bits/stl_construct.h (_Construct, _Construct_novalue): Also cast away cv-qualifiers when converting pointer to void. * testsuite/20_util/allocator/void.cc: Test construct function with cv-qualified types. (cherry picked from commit fc6f1128ae603164aea6303ce2b3ed0b57e6a378) Diff: --- libstdc++-v3/include/bits/stl_construct.h | 4 ++-- libstdc++-v3/testsuite/20_util/allocator/void.cc | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h index e53ed0d9f91..f14fba0cfb1 100644 --- a/libstdc++-v3/include/bits/stl_construct.h +++ b/libstdc++-v3/include/bits/stl_construct.h @@ -116,7 +116,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return; } #endif - ::new(static_cast(__p)) _Tp(std::forward<_Args>(__args)...); + ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); } #else template @@ -132,7 +132,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline void _Construct_novalue(_T1* __p) - { ::new(static_cast(__p)) _T1; } + { ::new((void*)__p) _T1; } template _GLIBCXX20_CONSTEXPR void diff --git a/libstdc++-v3/testsuite/20_util/allocator/void.cc b/libstdc++-v3/testsuite/20_util/allocator/void.cc index e3d024d525b..3720774abaf 100644 --- a/libstdc++-v3/testsuite/20_util/allocator/void.cc +++ b/libstdc++-v3/testsuite/20_util/allocator/void.cc @@ -87,8 +87,23 @@ static_assert( std::is_same::const_pointer, const void*>(), "const_pointer is const void*" ); #endif // C++20 +void +test02() +{ + std::allocator av; + int* p = std::allocator().allocate(1); + const int* c = p; + std::allocator_traits>::construct(av, c, 0); + volatile int* v = p; + std::allocator_traits>::construct(av, v, 0); + const volatile int* cv = p; + std::allocator_traits>::construct(av, cv, 0); + std::allocator().deallocate(p, 1); +} + int main() { test01(); + test02(); }