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: 63583 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp65674wrd; Thu, 2 Mar 2023 12:57:18 -0800 (PST) X-Google-Smtp-Source: AK7set+E0wF40baDCc8dM7tTeJMIJfQgVZiRmdnTTMbSOEnoW4aL4DXQ+CeNxKNey9ndy4WwmafE X-Received: by 2002:a17:906:33d0:b0:883:3c6e:23eb with SMTP id w16-20020a17090633d000b008833c6e23ebmr13398237eja.42.1677790638825; Thu, 02 Mar 2023 12:57:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677790638; cv=none; d=google.com; s=arc-20160816; b=VIPiast8oR7i4hrmLUMyzeHLhY4pKkFmDtHF7lk97syh2ynQahtj4661W8RBu1LxJh 6mQPVkwSFA+dP8V82tofIBmJP/dTuC2+EKkvw47m85qA9T4tefDvmomlu0VMHFAUHG16 B9LcEK3+p0jaRKwhAoQ+fKZIZVTfqWNYZkLQ7Ae52/jU1YY162tbise672bQ04raeshK E473XKYtbfIrjpq52nwYlN7c5A/V0m1dAh+p9LNa1vz3YqbtNKcOdcNh3zQC341Y2iez TsAtwlmOlxjZh07C8X5S5taiXIzYshZIRXb0EiwTE4U8N3AyPTopCUAXAO5v0x1lA0xv 03rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=81NMGmfPNCNj+hKWiIPx5S2j0NvrrVtLrQyIv8C2418=; b=r/FmXDGtrrOgzLFEZqyomsQcqM5Ue3FTLA367ZTsLSDQvBrOf6ie4mMKBmjYa7UMtJ 2ZegPsu6J3es0Oo4+as2PhdPQnx6xUoITDXBYiNU23wct/dHdFj4eY3XoyxJANvmOAlA hNjsVHC2OymBYbd+h7Y0o4kOxY+IvjM6nl3z0bCNeY/bY7UznuZQhtRPjNVUuqsnmFg1 ZotX7YEMYM0uPNtsWKfPJufbJfY/toeySvTLeOuo9I9VT08B0R/RlC04OFG+2vKNFQ3v 7QA7Z+s/kmP2tbizAASIC7DsKnb2hAUpw5RjHbC3dDFiuBtMBhTT2uQws5qCXw+qoNVO oqLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dWHqUo1I; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org ([2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id p7-20020a170906b20700b008b17fe8a06bsi333667ejz.70.2023.03.02.12.57.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 12:57:18 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dWHqUo1I; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0489C3858436 for ; Thu, 2 Mar 2023 20:57:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0489C3858436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677790627; 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=dWHqUo1IdlQ59TL0hzTzky8TeYBAkbAB8JS91z7j7EsmR6FHBGbvpOv7SH7/Ba46z YV/OgoSU6bNY4Ig6vsgYPymf/pd22oGpR/G7arFArO9xF41YUfMQSbYdahQXkJZD8y yK3deLrILXXmcK+DNF9VvdKs35xiCImzCA2prcAY= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759290996834393523?= X-GMAIL-MSGID: =?utf-8?q?1759290996834393523?= 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. +

+