[llvm] [mlir] [libc] [libcxx] [clang] [flang] [libc++][memory] P1132R8: out_ptr - a scalable output pointer abstraction (PR #73618)

Louis Dionne via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 14 08:31:14 PST 2023


================
@@ -0,0 +1,201 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <memory>
+
+// [inout.ptr], function template inout_ptr
+// template<class Pointer = void, class Smart, class... Args>
+//   auto inout_ptr(Smart& s, Args&&... args);                 // since c++23
+
+#include <cassert>
+#include <memory>
+
+#include "../types.h"
+
+// Test helpers.
+
+void replace_int_p(int** pp) {
+  assert(**pp == 90);
+  delete *pp;
+  *pp = new int{84};
+}
+
+void replace_int_p_with_nullptr(int** pp) {
+  assert(**pp == 90);
+  delete *pp;
+  *pp = nullptr;
+}
+
+void replace_nullptr_with_int_p(int** pp) {
+  assert(*pp == nullptr);
+  *pp = new int{84};
+}
+
+void replace_int_void_p(void** pp) {
+  assert(*(static_cast<int*>(*pp)) == 90);
+  delete static_cast<int*>(*pp);
+  *pp = new int{84};
+}
+
+void replace_int_void_p_with_nullptr(void** pp) {
+  assert(*(static_cast<int*>(*pp)) == 90);
+  delete static_cast<int*>(*pp);
+  *pp = nullptr;
+}
+
+void replace_nullptr_with_int_void_p(void** pp) {
+  assert(*pp == nullptr);
+  *pp = new int{84};
+}
+
+void replace_SomeInt_p(SomeInt** pp) {
+  auto si = **pp;
+  assert(si.value == 90);
+  delete static_cast<SomeInt*>(*pp);
+  *pp = new SomeInt{9084};
+}
+
+void replace_SomeInt_void_p(void** pp) {
+  assert(reinterpret_cast<SomeInt*>(*pp)->value == 90);
+  delete static_cast<SomeInt*>(*pp);
+  *pp = reinterpret_cast<void*>(new SomeInt{9084});
+}
+
+// Test `std::inout_ptr()` function.
+
+void test_raw_ptr() {
----------------
ldionne wrote:

I would suggest the following organization instead:

```c++
void test() {
  // Test with <explain in plain english>
  {
    auto f(int** pp) -> void {
      assert(**pp == 90);
      delete *pp;
      *pp = new int{84};
    };

    // raw pointer
    {
      int* ptr = new int{90};
      f(std::inout_ptr<int*>(ptr));
      assert(*ptr == 84);
      delete ptr;
    }
    // unique_ptr
    {
      std::unique_ptr<int> ptr = std::make_unique<int>(90);
      f(std::inout_ptr<int*>(ptr));
      assert(*ptr == 84);
    }
  }

  // ... same test for other C style functions ...
}
```

This localizes what you're testing a bit more and IMO it's easier to read. WDYT?

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


More information about the cfe-commits mailing list