[libcxx-commits] [libcxx] [libcxx] implement LWG4148: unique_ptr::operator* should not allow dangling references (PR #128213)

Mohamed Atef via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 8 14:46:49 PDT 2026


================
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++11
+
+// <memory>
+
+#include <memory>
+
+struct deleter {
+  using pointer = long*;
+  void operator()(pointer) const {}
+};
+
+void test() {
+  long l = 0;
+  std::unique_ptr<const int, deleter> p(&l);
+  // expected-error-re@*:* {{static assertion failed{{.*}}'!__reference_converts_from_temporary(const int &, long &)': Reference type _Tp must not convert from a temporary object}}
+  // expected-error@*:*{{returning reference to local temporary object}}
----------------
elhewaty wrote:

@frederick-vs-ja , It is the pre-C++26 solution, I can't get the static assertion to work! every time I test, `lit` doesn't expect our assertion.

```
# .---command stderr------------
# | error: 'expected-error' diagnostics expected but not seen: 
# |   File * Line * (directive at /home/mohamed/Desktop/open-source/llvm/llvm-project/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.observers/dereference.temporary.verify.cpp:23): static assertion failed{{.*}}the returned reference must not bind to a temporary object
# | 1 error generated.
# `-----------------------------
# error: command failed with exit status: 1

--

********************
********************
Failed Tests (1):
  llvm-libc++-shared.cfg.in :: std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.observers/dereference.temporary.verify.cpp
```

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


More information about the libcxx-commits mailing list