[libcxx-commits] [libcxx] ad79455 - [libc++] Don't pass the allocator in substr()

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 29 10:40:42 PST 2022


Author: Nikolas Klauser
Date: 2022-11-29T19:40:37+01:00
New Revision: ad79455fad9fcd798e1788801f579b87a3cb29bc

URL: https://github.com/llvm/llvm-project/commit/ad79455fad9fcd798e1788801f579b87a3cb29bc
DIFF: https://github.com/llvm/llvm-project/commit/ad79455fad9fcd798e1788801f579b87a3cb29bc.diff

LOG: [libc++] Don't pass the allocator in substr()

This bug was dicoved when implementing P2438R2.

Fixes #57190

Reviewed By: ldionne, Mordante, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D138069

Added: 
    

Modified: 
    libcxx/include/string
    libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string b/libcxx/include/string
index b36d83f5e6dc..bcd38b101b3e 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -1430,21 +1430,20 @@ public:
 
     _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
 
-    // TODO: Maybe don't pass in the allocator. See https://llvm.org/PR57190
 #if _LIBCPP_STD_VER <= 20
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
     basic_string substr(size_type __pos = 0, size_type __n = npos) const {
-      return basic_string(*this, __pos, __n, __alloc());
+      return basic_string(*this, __pos, __n);
     }
 #else
     _LIBCPP_HIDE_FROM_ABI constexpr
     basic_string substr(size_type __pos = 0, size_type __n = npos) const& {
-      return basic_string(*this, __pos, __n, __alloc());
+      return basic_string(*this, __pos, __n);
     }
 
     _LIBCPP_HIDE_FROM_ABI constexpr
     basic_string substr(size_type __pos = 0, size_type __n = npos) && {
-      return basic_string(std::move(*this), __pos, __n, __alloc());
+      return basic_string(std::move(*this), __pos, __n);
     }
 #endif
 

diff  --git a/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp b/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp
index 7f6404abd826..d62067cf13a5 100644
--- a/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp
@@ -16,6 +16,7 @@
 #include <algorithm>
 #include <cassert>
 
+#include "test_allocator.h"
 #include "test_macros.h"
 #include "min_allocator.h"
 
@@ -119,11 +120,37 @@ TEST_CONSTEXPR_CXX20 bool test() {
   return true;
 }
 
+TEST_CONSTEXPR_CXX20 bool test_alloc() {
+  {
+    using alloc  = test_allocator<char>;
+    using string = std::basic_string<char, std::char_traits<char>, alloc>;
+    test_allocator_statistics stats;
+    {
+      string str((alloc(&stats)));
+      stats = test_allocator_statistics();
+      (void)str.substr();
+      assert(stats.moved == 0);
+      assert(stats.copied == 0);
+    }
+    {
+      string str((alloc(&stats)));
+      stats = test_allocator_statistics();
+      (void)std::move(str).substr();
+      assert(stats.moved == 0);
+      assert(stats.copied == 0);
+    }
+  }
+
+  return true;
+}
+
 int main(int, char**)
 {
   test();
+  test_alloc();
 #if TEST_STD_VER > 17
   static_assert(test());
+  static_assert(test_alloc());
 #endif
 
   return 0;


        


More information about the libcxx-commits mailing list