Debian Codegraph @

libstdc++-v3/testsuite/20_util/unique_ptr/lwg4148.cc

raw
1 // { dg-do compile { target c++11 } }
2 // { dg-additional-options "-Wsystem-headers" }
3
4 // LWG 4148. unique_ptr::operator* should not allow dangling references
5
6 #include <memory>
7
8 struct pointer
9 {
10 pointer() { }
11 pointer(std::nullptr_t) { }
12 int operator*() const { return 0; }
13 bool operator==(pointer) const { return true; }
14 bool operator==(std::nullptr_t) const { return false; }
15 #ifndef __cpp_lib_three_way_comparison
16 bool operator!=(pointer) const { return false; }
17 bool operator!=(std::nullptr_t) const { return true; }
18 #endif
19 };
20
21 struct Deleter
22 {
23 using pointer = ::pointer;
24 void operator()(pointer) const { }
25 };
26
27 std::unique_ptr<const int, Deleter> up;
28 int i = *up; // { dg-error "here" }
29 // { dg-error "dangling reference" "" { target *-*-* } 0 }
30
31 // { dg-warning "returning reference to temporary" "" { target c++23_down } 0 }
32 // { dg-error "returning reference to temporary" "" { target c++26 } 0 }