public inbox for
help / color / mirror / Atom feed
* gcc-wwwdocs branch master updated. 7c5925997a1a6fef98e29c7a4ed946924ec23df8
@ 2023-03-02 20:55 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-03-02 20:55 UTC (permalink / raw)
  To: gcc-cvs-wwwdocs

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gcc-wwwdocs".

The branch, master has been updated
       via  7c5925997a1a6fef98e29c7a4ed946924ec23df8 (commit)
      from  20a280fdead95b9a6e9071a6d167dac76cba616a (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 7c5925997a1a6fef98e29c7a4ed946924ec23df8
Author: Jonathan Wakely <>
Date:   Thu Mar 2 20:54:46 2023 +0000

    Document allocator_traits<A>::rebind_alloc assertion with GCC 13

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 <code>-fexcess-precision=fast</code> option can be used to request the
 previous behavior.
+<h3 id="alloc-rebind">allocator_traits&lt;A&gt;::rebind_alloc&lt;A::value_type&gt; must be A</h3>
+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 <tt>Alloc&lt;T&gt;</tt>
+cannot be correctly rebound to another type <tt>Alloc&lt;U&gt;</tt>,
+you will get an error like this:
+.../bits/alloc_traits.h:70:31: error: static assertion failed: allocator_traits&lt;A&gt;::rebind_alloc&lt;A::value_type&gt; 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 <tt>rebind</tt> 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 <tt>Alloc&lt;T&gt;</tt>
+that derives from <tt>std::allocator&lt;T&gt;</tt> but does not provide its own
+<tt>rebind</tt> member. When the standard library attempts to rebind the
+allocator using <tt>Alloc&lt;T&gt;::rebind&lt;U&gt;</tt> it finds the
+<tt>std::allocator&lt;T&gt;::rebind&lt;U&gt;</tt> member from the base class,
+and the result is <tt>std::allocator&lt;U&gt;</tt> instead of
+The solution is to provide a correct <tt>rebind</tt> member as shown below.
+A converting constructor must also be provided, so that that an
+<tt>Alloc&lt;U&gt;</tt> can be constructed from an <tt>Alloc&lt;T&gt;</tt>,
+and vice versa:
+template&lt;class T&gt;
+class Alloc
+  Alloc();
+  <b>
+  template&lt;class U&gt; Alloc(const Alloc&lt;U&gt;);
+  template&lt;class U&gt; struct rebind { using other = Alloc&lt;U&gt;; };
+  </b>
+  // ...
+Since C++20, there is no <tt>rebind</tt> member in <tt>std::allocator</tt>,
+so deriving your own allocator types from <tt>std::allocator</tt> is simpler
+and doesn't require the derived allocator to provide its own <tt>rebind</tt>.
+For compatibility with previous C++ standards, the member should still be
+provided. The converting constructor is still required even in C++20.


Summary of changes:
 htdocs/gcc-13/porting_to.html | 60 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-02 20:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-02 20:55 gcc-wwwdocs branch master updated. 7c5925997a1a6fef98e29c7a4ed946924ec23df8 Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).