[libcxx-commits] [libcxx] [libcxx][string] Test: default constructed allocators can be unequal (PR #195839)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 25 05:01:04 PDT 2026


================
@@ -59,31 +59,33 @@ TEST_CONSTEXPR_CXX20 void test2() {
   {
 #  if TEST_STD_VER > 14
     static_assert((noexcept(S{})), "");
-#  elif TEST_STD_VER >= 11
+#  else
     static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "");
 #  endif
     S s;
     LIBCPP_ASSERT(s.__invariants());
     assert(s.data());
     assert(s.size() == 0);
     assert(s.capacity() >= s.size());
-    assert(s.get_allocator() == typename S::allocator_type());
+    static_assert(std::is_same<decltype(s.get_allocator()), typename S::allocator_type>::value, "");
+    ASSERT_CONTAINER_ALLOCATOR_EQUALS_DEFAULT(S, s);
----------------
ldionne wrote:

So basically what's going on here is that:

1. `std::string::string()` creates a string with `Allocator()`.
2. You get a copy of that allocator with `s.get_allocator()` (and you know the copy must compare equal to the originally created allocator).
3. However, since there is no requirement that `Allocator() == Allocator()`, there's no way to extract back the "original" value of the allocator created in the default constructor.

If I got that right, then I think the right call here is to drop this assertion altogether. Furthermore, the `static_assert` about the return type of `s.get_allocator()` belongs in `get_allocator.pass.cpp`, not here: so I'd remove these two added lines.

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


More information about the libcxx-commits mailing list