| 1 |
// PR c++/99859 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
|
| 4 |
template <class T> |
| 5 |
struct intrusive_ptr |
| 6 |
{ |
| 7 |
T *ptr = nullptr; |
| 8 |
constexpr explicit intrusive_ptr(T* p) : ptr(p) { |
| 9 |
++ptr->count_; |
| 10 |
} |
| 11 |
constexpr ~intrusive_ptr() { |
| 12 |
if (ptr->dec() == 0) |
| 13 |
delete ptr; |
| 14 |
} |
| 15 |
constexpr intrusive_ptr(intrusive_ptr const& a) : ptr(a.ptr) { |
| 16 |
++ptr->count_; |
| 17 |
} |
| 18 |
}; |
| 19 |
|
| 20 |
struct Foo { |
| 21 |
int count_ = 0; |
| 22 |
constexpr int dec() { |
| 23 |
return --count_; |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
constexpr bool baz() { |
| 28 |
Foo f { 4 }; |
| 29 |
intrusive_ptr a(&f); |
| 30 |
return true; |
| 31 |
} |
| 32 |
constexpr bool x = baz(); |
| 33 |
|
| 34 |
constexpr void bar(intrusive_ptr<Foo> a) |
| 35 |
{ |
| 36 |
if (a.ptr->count_ != 2) throw 1; |
| 37 |
} |
| 38 |
|
| 39 |
constexpr bool foo() { |
| 40 |
intrusive_ptr a(new Foo()); |
| 41 |
bar(a); |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
static_assert(foo()); |