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.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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677790577; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=81NMGmfPNCNj+hKWiIPx5S2j0NvrrVtLrQyIv8C2418=; b=X4skszqpzWiP6/XyuhL6VAQr9OgkBf4utH59qpyk+7reYaUWNgxcCDjjPyCfBiuwWBhcc8 bRqAOq4UYmcsaqTGG+E/jkg20s6jnIxwFXesEZJcKQT752l3PnIb5Nhr+vjsbiQ8uqNoJU yPUreKPHIMLyfzIiiH+MUZdJ3vEgdig= 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) From: Jonathan Wakely 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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 List-Id: 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. +

+ -- 2.39.2