aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp2a/constexpr-new18.C
blob: 24b298aafd45a002e06aebe4f00f63d9a434929e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// PR c++/99859
// { dg-do compile { target c++20 } }

template <class T>
struct intrusive_ptr
{
  T *ptr = nullptr;
  constexpr explicit intrusive_ptr(T* p) : ptr(p) {
    ++ptr->count_;
  }
  constexpr ~intrusive_ptr() {
    if (ptr->dec() == 0)
      delete ptr;
  }
  constexpr intrusive_ptr(intrusive_ptr const& a) : ptr(a.ptr) {
    ++ptr->count_;
  }
};

struct Foo {
  int count_ = 0;
  constexpr int dec() {
    return --count_;
  }
};

constexpr bool baz() {
  Foo f { 4 };
  intrusive_ptr a(&f);
  return true;
}
constexpr bool x = baz();

constexpr void bar(intrusive_ptr<Foo> a) 
{
  if (a.ptr->count_ != 2) throw 1;
}

constexpr bool foo() {
  intrusive_ptr a(new Foo());
  bar(a);
  return true;
}

static_assert(foo());