[libcxx-commits] [libcxx] [libc++][memory] LWG4148: `unique_ptr::operator*` should not allow dangling references (PR #214971)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Aug 9 21:37:16 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;
----------------
Zingam wrote:
@frederick-vs-ja The idea was to use the same example from the LWG but I guess simpler is better. Thank you!
https://github.com/llvm/llvm-project/pull/214971
More information about the libcxx-commits
mailing list