[libcxx-commits] [libcxx] [libcxx] improves diagnostics for containers with bad value types (PR #106296)

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 28 11:29:39 PDT 2024


================
@@ -76,8 +79,16 @@ struct __non_trivial_if<true, _Unique> {
 
 template <class _Tp>
 class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {
-  static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
-  static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
+  static_assert(!is_const<_Tp>::value, "'std::allocator' can only allocate non-const object types");
+  static_assert(!is_volatile<_Tp>::value, "'std::allocator' can only allocate non-volatile object types");
+  static_assert(!is_reference<_Tp>::value || !is_function<typename remove_reference<_Tp>::type>::value,
+                "'std::allocator' can only allocate object types; function references are not objects (consider using "
+                "a function pointer)");
+  static_assert(!is_reference<_Tp>::value,
+                "'std::allocator' can only allocate object types; references are not objects");
----------------
cjdb wrote:

Folks internally suggested that I change the text to the following, as they feel it'll be more helpful than what I'm currently proposing.

* `std::allocator<T const>`: `'std::allocator' cannot allocate const objects` (return to status quo)
* `std::allocator<T volatile>`: `'std::allocator' cannot allocate volatile objects` (return to status quo)
* `std::allocator<T&>`: `'std::allocator' cannot allocate references`
* `std::allocator<T()>`: `'std::allocator' cannot allocate functions`

We do lose the function reference suggestion, but I'm okay with doing that for something that's more helpful.

https://github.com/llvm/llvm-project/pull/106296


More information about the libcxx-commits mailing list