From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 028463858401 for ; Fri, 14 Jan 2022 10:16:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 028463858401 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-317-cX1YrdPLN2idtOUTk2bOrA-1; Fri, 14 Jan 2022 05:16:13 -0500 X-MC-Unique: cX1YrdPLN2idtOUTk2bOrA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C61D11083F61; Fri, 14 Jan 2022 10:16:11 +0000 (UTC) Received: from localhost (unknown [10.33.36.252]) by smtp.corp.redhat.com (Postfix) with ESMTP id 759C010595B1; Fri, 14 Jan 2022 10:16:11 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Ignore cv-quals when std::allocator constructs Date: Fri, 14 Jan 2022 10:16:10 +0000 Message-Id: <20220114101610.2975321-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Jan 2022 10:16:18 -0000 Tested powerpc64le-linux, pushed to trunk. 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. --- 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 7c5fd4c9cf7..9531222809c 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 52e1fef3700..5cdf0be012c 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(); } -- 2.31.1