[libcxx-commits] [libcxx] [libc++][memory] LWG4148: `unique_ptr::operator*` should not allow dangling references (PR #214971)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 9 18:49:56 PDT 2026


================
@@ -12,13 +12,32 @@
 
 // test op*()
 
+// XFAIL: FROZEN-CXX03-HEADERS-FIXME
+
 #include <memory>
 
 #include "test_macros.h"
 
-void f() {
+void test() {
   std::unique_ptr<int[]> p(new int(3));
   const std::unique_ptr<int[]>& cp = p;
   TEST_IGNORE_NODISCARD(*p);  // expected-error-re {{indirection requires pointer operand ('std::unique_ptr<int{{[ ]*}}[]>' invalid)}}
   TEST_IGNORE_NODISCARD(*cp); // expected-error-re {{indirection requires pointer operand ('const std::unique_ptr<int{{[ ]*}}[]>' invalid)}}
 }
+
+void test_lwg4196() {
+  struct Deleter {
+    using pointer = long*;
+
+    void operator()(pointer) const {}
+  };
+
+  long l = 0;
+  std::unique_ptr<const int, Deleter> p(&l);
+
+  // expected-error-re@*:* {{static assertion failed due to requirement {{.+}}The returned reference must not bind to a temporary object.}}
+#if TEST_STD_VER >= 26
+  // expected-error@*:* {{returning reference to local temporary object}}
+#endif
+  [[maybe_unused]] int i = *p;
----------------
frederick-vs-ja wrote:

Would it be better to simply these a bit as the following?
```suggestion
  std::unique_ptr<const int, Deleter> p;

  // expected-error-re@*:* {{static assertion failed due to requirement {{.+}}The returned reference must not bind to a temporary object.}}
#if TEST_STD_VER >= 26
  // expected-error@*:* {{returning reference to local temporary object}}
#endif
  (void)*p;
```

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


More information about the libcxx-commits mailing list