From patchwork Thu Mar 2 20:56:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1751018 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=HdYnR1rU; dkim-atps=neutral Received: from sourceware.org (unknown [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PSNgx5sjQz2467 for ; Fri, 3 Mar 2023 07:56:44 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A4AAB385B515 for ; Thu, 2 Mar 2023 20:56:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4AAB385B515 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677790602; bh=81NMGmfPNCNj+hKWiIPx5S2j0NvrrVtLrQyIv8C2418=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HdYnR1rU4XK++AWaIZRAV68klv0zQQtLCJDWVDcQTQx5RNzgvrhu9qonRKabWb/K3 snputzOsJOKPwadcBMe0pCIjJ0/QzmnWJRyI0ngjITkTKr8ctsb2pywI6otnMQgjFE 1DILVKX1xee/hvkvRENhMzR6rEUUkXi0ujR9Ry7Y= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 2DBB23858CDB for ; Thu, 2 Mar 2023 20:56:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DBB23858CDB Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-354-BDCYpuCGM8mdNFYrYWRvDw-1; Thu, 02 Mar 2023 15:56:15 -0500 X-MC-Unique: BDCYpuCGM8mdNFYrYWRvDw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 23D5418E526F; Thu, 2 Mar 2023 20:56:15 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id D1CAF1121315; Thu, 2 Mar 2023 20:56:14 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org Subject: [wwwdocs] Document allocator_traits::rebind_alloc assertion with GCC 13 Date: Thu, 2 Mar 2023 20:56:14 +0000 Message-Id: <20230302205614.1564709-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Pushed to wwwdocs. --- htdocs/gcc-13/porting_to.html | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/htdocs/gcc-13/porting_to.html b/htdocs/gcc-13/porting_to.html index 5cbeefb6..f0ccef69 100644 --- a/htdocs/gcc-13/porting_to.html +++ b/htdocs/gcc-13/porting_to.html @@ -144,5 +144,65 @@ done in the i387 floating point stack or are spilled from it. The -fexcess-precision=fast option can be used to request the previous behavior. +

allocator_traits<A>::rebind_alloc<A::value_type> must be A

+ +

+GCC 13 now checks that allocators used with the standard library +can be "rebound" to allocate memory for a different type, +as required by the allocator requirements in the C++ standard. +If an allocator type Alloc<T> +cannot be correctly rebound to another type Alloc<U>, +you will get an error like this: +

+ +
+.../bits/alloc_traits.h:70:31: error: static assertion failed: allocator_traits<A>::rebind_alloc<A::value_type> must be A
+
+ +

+The assertion checks that rebinding an allocator to its own value type is a +no-op, which will be true if its rebind member is defined correctly. +If rebinding it to its own value type produces a different type, +then the allocator cannot be used with the standard library. +

+ +

+The most common cause of this error is an allocator type Alloc<T> +that derives from std::allocator<T> but does not provide its own +rebind member. When the standard library attempts to rebind the +allocator using Alloc<T>::rebind<U> it finds the +std::allocator<T>::rebind<U> member from the base class, +and the result is std::allocator<U> instead of +Alloc<U>. +

+ +

+The solution is to provide a correct rebind member as shown below. +A converting constructor must also be provided, so that that an +Alloc<U> can be constructed from an Alloc<T>, +and vice versa: +

+

+template<class T>
+class Alloc
+{
+  Alloc();
+  
+  template<class U> Alloc(const Alloc<U>);
+
+  template<class U> struct rebind { using other = Alloc<U>; };
+  
+  // ...
+};
+
+ +

+Since C++20, there is no rebind member in std::allocator, +so deriving your own allocator types from std::allocator is simpler +and doesn't require the derived allocator to provide its own rebind. +For compatibility with previous C++ standards, the member should still be +provided. The converting constructor is still required even in C++20. +

+