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

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 29 14:20:56 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");
----------------
philnik777 wrote:

When you have a `const int&` you get the information that you have to remove both the `const` and the `&` at once, not only after you've removed one or the other. We also avoid three instantiations for every type we have a `vector` of. Passing this test is the common case after all.

Is cv-qualified really that uncommon/niche? When you look at the cppreference page for `const` you get basically redirected to https://en.cppreference.com/w/cpp/language/cv. FWIW you could also say "const or volatile qualified". It's a bit more verbose, but should be perfectly understandable.

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


More information about the libcxx-commits mailing list