From: Jonathan Wakely <jwakely@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org
Subject: [wwwdocs] Document allocator_traits<A>::rebind_alloc assertion with GCC 13
Date: Thu, 2 Mar 2023 20:56:14 +0000 [thread overview]
Message-ID: <20230302205614.1564709-1-jwakely@redhat.com> (raw)
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 <code>-fexcess-precision=fast</code> option can be used to request the
previous behavior.
+<h3 id="alloc-rebind">allocator_traits<A>::rebind_alloc<A::value_type> must be A</h3>
+
+<p>
+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<T></tt>
+cannot be correctly rebound to another type <tt>Alloc<U></tt>,
+you will get an error like this:
+</p>
+
+<pre>
+.../bits/alloc_traits.h:70:31: error: static assertion failed: allocator_traits<A>::rebind_alloc<A::value_type> must be A
+</pre>
+
+<p>
+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.
+</p>
+
+<p>
+The most common cause of this error is an allocator type <tt>Alloc<T></tt>
+that derives from <tt>std::allocator<T></tt> but does not provide its own
+<tt>rebind</tt> member. When the standard library attempts to rebind the
+allocator using <tt>Alloc<T>::rebind<U></tt> it finds the
+<tt>std::allocator<T>::rebind<U></tt> member from the base class,
+and the result is <tt>std::allocator<U></tt> instead of
+<tt>Alloc<U></tt>.
+</p>
+
+<p>
+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<U></tt> can be constructed from an <tt>Alloc<T></tt>,
+and vice versa:
+</p>
+<pre><code>
+template<class T>
+class Alloc
+{
+ Alloc();
+ <b>
+ template<class U> Alloc(const Alloc<U>);
+
+ template<class U> struct rebind { using other = Alloc<U>; };
+ </b>
+ // ...
+};
+</code></pre>
+
+<p>
+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.
+</p>
+
</body>
</html>
--
2.39.2
next reply other threads:[~2023-03-02 20:56 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-02 20:56 Jonathan Wakely [this message]
2023-03-03 22:47 ` Gerald Pfeifer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230302205614.1564709-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).