aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp23/constexpr-nonlit8.C
blob: 3fb1b93bd0739fefe7f8a16cf2714e8b02ddc0cd (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// PR c++/106649
// P2448 - Relaxing some constexpr restrictions
// { dg-do compile { target c++14 } }
// { dg-options "" }

// No constexpr constructors = not a literal type.
struct NonLiteral {
  NonLiteral() {}
};

// C++23: It is possible to write a constexpr function for which no
// invocation satisfies the requirements of a core constant expression.
constexpr NonLiteral
fn0 (int) // { dg-warning "invalid return type" "" { target c++20_down } }
{
  return NonLiteral{};
}

constexpr int
fn1 (NonLiteral) // { dg-warning "invalid type" "" { target c++20_down } }
{
  return 42;
}

// From P2448.
void f(int& i) {
    i = 0;
}

constexpr void g(int& i) {
    f(i); // { dg-warning "call to" "" { target c++20_down } }
}

// [dcl.constexpr] used to have this.
constexpr int f(bool b)
  { return b ? throw 0 : 0; }           // OK
constexpr int f() { return f(true); }   // ill-formed, no diagnostic required

struct B {
  constexpr B(int) : i(0) { }
  int i;
};

int global;

struct D : B {
  constexpr D() : B(global) { } // { dg-warning "not usable" "" { target c++20_down } }
  // ill-formed, no diagnostic required
  // lvalue-to-rvalue conversion on non-constant global
};

// If no specialization of the template would satisfy the requirements
// for a constexpr function when considered as a non-template function,
// the template is ill-formed, no diagnostic required.
template<typename>
constexpr void
fn2 ()
{
  int i = 42;
  f (i);
}

void
fn3 ()
{
  fn2<int>();
}

constexpr volatile int cvi = 10;

constexpr int
fn4 ()
{
  return cvi;  // { dg-warning "lvalue-to-rvalue conversion" "" { target c++20_down } }
}

constexpr unsigned int
fn5 (int *p)
{
  unsigned int *q = reinterpret_cast<unsigned int *>(p); // { dg-warning "reinterpret_cast" "" { target c++20_down } }
  return *q;
}

constexpr int
fn6 (int i)
{
  void *p = (void *) 1LL; // { dg-warning ".reinterpret_cast. from integer to pointer" "" { target c++20_down } }
  return 42;
}

constexpr int
fn7 (int i)
{
  static int s = i; // { dg-error "static" "" { target c++20_down } }
  return s;
}