Message ID | 20230302205614.1564709-1-jwakely@redhat.com |
---|---|
State | New |
Headers | show |
Series | [wwwdocs] Document allocator_traits<A>::rebind_alloc assertion with GCC 13 | expand |
On Thu, 2 Mar 2023, Jonathan Wakely via Gcc-patches wrote: > Pushed to wwwdocs. Thank you! > +If an allocator type <tt>Alloc<T></tt> Note that HTML 5 complains about the use of <tt> and we are using <code> instead. I just pushed the following patch addressing that. (The diff looks a bit bigger due to changes re line breaks.) Gerald commit 5a75fbda8c3c647b2ef659ffe67a031ee957abe6 Author: Gerald Pfeifer <gerald@pfeifer.com> Date: Fri Mar 3 23:41:36 2023 +0100 gcc-13: Use <code> instead of <tt> diff --git a/htdocs/gcc-13/porting_to.html b/htdocs/gcc-13/porting_to.html index 953e1453..733bb254 100644 --- a/htdocs/gcc-13/porting_to.html +++ b/htdocs/gcc-13/porting_to.html @@ -150,8 +150,8 @@ previous behavior. 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>, +If an allocator type <code>Alloc<T></code> +cannot be correctly rebound to another type <code>Alloc<U></code>, you will get an error like this: </p> @@ -161,26 +161,27 @@ you will get an error like this: <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. +no-op, which will be true if its <code>rebind</code> 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>. +The most common cause of this error is an allocator type +<code>Alloc<T></code> that derives from +<code>std::allocator<T></code> but does not provide its own +<code>rebind</code> member. When the standard library attempts to rebind the +allocator using <code>Alloc<T>::rebind<U></code> it finds the +<code>std::allocator<T>::rebind<U></code> member from the base +class, and the result is <code>std::allocator<U></code> instead of +<code>Alloc<U></code>. </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: +The solution is to provide a correct <code>rebind</code> member as shown +below. A converting constructor must also be provided, so that that an +<code>Alloc<U></code> can be constructed from an +<code>Alloc<T></code>, and vice versa: </p> <pre><code> template<class T> @@ -197,9 +198,10 @@ class Alloc </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>. +Since C++20, there is no <code>rebind</code> member in +<code>std::allocator</code>, so deriving your own allocator types from +<code>std::allocator</code> is simpler and doesn't require the derived +allocator to provide its own <code>rebind</code>. For compatibility with previous C++ standards, the member should still be provided. The converting constructor is still required even in C++20. </p>
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>