[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
Fri Aug 30 01:11:38 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:

I would be against it if that means even more classes being instantiated, but maybe I found something we'd both be happy with. Louis already wants a centralized place for the checks (which I agree would be a good idea), so maybe we could have
```c++
template <class _Tp, bool = is_same<_Tp, __remove_cvref_t<_Tp> >::value>
struct __check_container_constraints {};

template <class _Tp>
struct __check_container_constraints<_Tp, false> {
  static_assert(!is_const<_Tp>::value, "cannot allocate `const` objects");
  static_assert(!is_volatile<_Tp>::value, "Some fancy diagnostic");
  // ...
};
```
That would avoid all the instantiations in the common case. I don't care about the instantiations here if it fails anyways.

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


More information about the libcxx-commits mailing list