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

Nikolas Klauser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 15 15:19:44 PST 2022


philnik created this revision.
philnik added reviewers: ldionne, Mordante.
Herald added a project: All.
philnik requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

This bug was dicoved when implementing P2438R2.

Fixes #57190


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138069

Files:
  libcxx/include/string
  libcxx/test/std/strings/basic.string/string.cons/substr.allocator.pass.cpp


Index: libcxx/test/std/strings/basic.string/string.cons/substr.allocator.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/strings/basic.string/string.cons/substr.allocator.pass.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <string>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+
+TEST_CONSTEXPR_CXX20 bool test() {
+  {
+    using alloc  = test_allocator<char>;
+    using string = std::basic_string<char, std::char_traits<char>, alloc>;
+    test_allocator_statistics stats;
+    {
+      string str((alloc(&stats)));
+      // Before C++17 the above constructor call may generate a call to the move constructor of the allocator
+      stats = test_allocator_statistics();
+      (void)str.substr();
+      assert(stats.moved == 0);
+      assert(stats.copied == 0);
+    }
+    {
+      string str((alloc(&stats)));
+      // Before C++17 the above constructor call may generate a call to the move constructor of the allocator
+      stats = test_allocator_statistics();
+      (void)std::move(str).substr();
+      assert(stats.moved == 0);
+      assert(stats.copied == 0);
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+#if TEST_STD_VER >= 20
+  static_assert(test());
+#endif
+
+  return 0;
+}
Index: libcxx/include/string
===================================================================
--- libcxx/include/string
+++ libcxx/include/string
@@ -1421,21 +1421,20 @@
 
     _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
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138069.475603.patch
Type: text/x-patch
Size: 2788 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221115/97e220db/attachment.bin>


More information about the libcxx-commits mailing list