[PATCH] libstdc++: Add allocate_at_least (P0401) [PR118030]

Jonathan Wakely jwakely.gcc@gmail.com
Mon Mar 9 13:27:10 GMT 2026


On Mon, 9 Mar 2026 at 10:01, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>> On 3/8/26 7:01 AM, Jonathan Wakely wrote:
>> > On Sat, 7 Mar 2026, 12:38 Nathan Myers, <ncm@cantrip.org
>> >     +       *  @return Memory of suitable size and alignment for @a n or
>> >     more
>> >     +       *  contiguous objects of type @c value_type .
>> >
>> >
>> > Please use markdown for new doxygen comments instead of doxygen's @c  or
>> > html like <tt>. In general only back ticks are needed, and they're more
>> > readable then the alternatives.
>> Good.
>>
>> >     +       *
>> >     +       *  Returns <tt> a.allocate_at_least(n) </tt> if that expression
>> >     +       *  is well-formed, else <tt> { a.allocate(n), n } </tt>. When an
>> >     +       *  allocator is obliged to reserve more space than required for
>> >     +       *  the cited @c n objects, it may deliver the extra space to the
>> >     +       *  caller.
>> >     +      */
>> >     +      [[nodiscard]] static constexpr auto
>> >     +      allocate_at_least(_Alloc& __a, size_type __n)
>> >     +       -> allocation_result<pointer, size_type>
>> >     +      {
>> >     +       static_assert(requires { sizeof(value_type); },
>> >     +         "allocated object type must be complete");
>> >
>> >
>> > Does using a requires-expression make a difference here, rather than
>> > just sizeof directly?
>>
>> Just that it writes out the error message attached.
>
>
> Ah yes, the requires-expression means we get a failed static_assert:
>
> /home/jwakely/gcc/16/include/c++/16.0.1/bits/new_allocator.h:131:23: error: static assertion failed: cannot allocate incomplete types
>  131 |         static_assert(requires { sizeof(_Tp); }, "cannot allocate incomplete types");
>      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~
>  * 'false' evaluates to false
>
> instead of an invalid one that doesn't even get as far as testing the boolean expression:
>
> /home/jwakely/gcc/16/include/c++/16.0.1/bits/new_allocator.h:131:23: error: invalid application of 'sizeof' to incomplete type 'S'
>  131 |         static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
>      |                       ^~~~~~~~~~~
>
> But then it would be better to improve the existing checks in allocator::allocate instead of adding another one here. That can be done separately, so for this patch please don't add a new static_assert, we already get a diagnostic from the allocate(n) call.

Maybe something like the attached patch.
-------------- next part --------------
commit 723a2a1b6ac2e77b4507bad03ec71714ce08e3d6
Author:     Jonathan Wakely <jwakely@redhat.com>
AuthorDate: Mon Mar 9 12:15:19 2026
Commit:     Jonathan Wakely <redi@gcc.gnu.org>
CommitDate: Mon Mar 9 13:24:57 2026

    libstdc++: Improve diagnostics for std::allocator<incomplete type>
    
    Using requires { sizeof(T); } in __new_allocator::allocate gives a
    better diagnostic when the static_assert fails.
    
    We can also reduce the total number of diagnostics due to invalid
    sizeof(T) and alignof(T) by using the same requires-expression to make
    the body of the function discarded statement when the static_assert
    already failed. This fixes a regression in diagnostic quality due to
    making __new_allocator::allocate constexpr in r16-7271-g7197d0cce70525,
    which caused a cascade of seven errors instead of just one for
    std::allocator<incomplete-type>().allocate(1).
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/allocator.h (allocator::allocate): Use specific
            feature test macro for constexpr allocate and deallocate. Make
            consteval path a discarded statement if sizeof(T) is ill-formed.
            * include/bits/new_allocator.h (__new_allocator::allocate): Use
            requires-expression for static_cast. Make function body a
            discarded stament if sizeof(T) is ill-formed. Use if-constexpr
            for alignment checks.

diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h
index 9f9526bd5b01..5f1905d6f5ca 100644
--- a/libstdc++-v3/include/bits/allocator.h
+++ b/libstdc++-v3/include/bits/allocator.h
@@ -194,6 +194,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       allocate(size_t __n)
       {
 	if (std::__is_constant_evaluated())
+#if __cpp_concepts
+	  if constexpr (requires { sizeof(_Tp); })
+#endif
 	  {
 	    if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
 	      std::__throw_bad_array_new_length();
diff --git a/libstdc++-v3/include/bits/new_allocator.h b/libstdc++-v3/include/bits/new_allocator.h
index 4fe67f99e5c0..edfed00fad28 100644
--- a/libstdc++-v3/include/bits/new_allocator.h
+++ b/libstdc++-v3/include/bits/new_allocator.h
@@ -128,9 +128,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #if __cplusplus >= 201103L
 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
 	// 3308. std::allocator<void>().allocate(n)
-	static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
+	static_assert(
+#if __cpp_concepts
+	  requires { sizeof(_Tp); },
+#else
+	  sizeof(_Tp) != 0,
+#endif
+	  "cannot allocate incomplete types");
 #endif
 
+#if __cplusplus >= 201103L && __cpp_concepts
+	if constexpr (!requires { sizeof(_Tp); })
+	  return nullptr; // static_assert already failed
+	else
+#endif
 	if (__builtin_expect(__n > this->_M_max_size(), false))
 	  {
 	    // _GLIBCXX_RESOLVE_LIB_DEFECTS
@@ -139,16 +150,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	      std::__throw_bad_array_new_length();
 	    std::__throw_bad_alloc();
 	  }
-
 #if __cpp_aligned_new && __cplusplus >= 201103L
-	if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+	else if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
 	  {
 	    std::align_val_t __al = std::align_val_t(alignof(_Tp));
 	    return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp),
 							   __al));
 	  }
 #endif
-	return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
+	else
+	  return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
       }
 
       // __p is not permitted to be a null pointer.
@@ -162,7 +173,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
 
 #if __cpp_aligned_new && __cplusplus >= 201103L
-	if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+	if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
 	  {
 	    _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),
 				     std::align_val_t(alignof(_Tp)));


More information about the Libstdc++ mailing list