aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp2a/consteval-prop2.C
blob: 30129a4a2666cea55baac620016d94ebd1bda315 (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
// P2564R3
// { dg-do compile { target c++20 } }
// Testcase from P2564R3.

consteval int id(int i) { return i; }
constexpr char id(char c) { return c; }

template<class T>
constexpr int f(T t) {
  return t + id(t);		// { dg-message "immediate-escalating expression .id\\(t\\)." }
}

auto a = &f<char>;              // OK, f<char> is not an immediate function
auto b = &f<int>;               // { dg-error "taking address of an immediate function" }

static_assert(f(3) == 6);       // OK

template<class T>
constexpr int g(T t) {          // g<int> is not an immediate function
  return t + id(42);            // because id(42) is already a constant
}

template<class T, class F>
constexpr bool is_not(T t, F f) {
  return not f(t);
}

consteval bool is_even(int i) { return i % 2 == 0; }

static_assert(is_not(5, is_even));      // OK

int x = 0;

template<class T>
constexpr T h(T t = id(x)) {    // h<int> is not an immediate function
    return t;
}

template<class T>
constexpr T hh() {              // hh<int> is an immediate function
  return h<T>();		// { dg-error "the value of .x. is not usable in a constant expression" }
// { dg-message "immediate-escalating expression .id\\(x\\)." "" { target *-*-* } .-1 }
}

int i = hh<int>();              // { dg-error "call to consteval function|called in a constant expression" }
				// error: hh<int>() is an immediate-escalating expression
                                // outside of an immediate-escalating function
struct A {
  int x;
  int y = id(x);
};

// [expr.const]#example-9 says:
//   k<int> is not an immediate function because A(42) is a
//   constant expression and thus not immediate-escalating
// In the evaluation of A(42), the member x has just been initialized
// to constant 42.  And A(42) is constant-evaluated because "An aggregate
// initialization is an immediate invocation if it evaluates a default
// member initializer that has a subexpression that is an
// immediate-escalating expression."
template<class T>
constexpr int k(int) {
  return A(42).y;
}

int
test (int i)
{
  int r = g (42) + g(i);
  int t = k<int>(42)
	    + k<int>(i); // { dg-bogus "call to|constant" "" { xfail *-*-* } }
  return r + t;
}

// Just like above, but make the call to id(x) actually a constant.
struct A2 {
  static constexpr int x = 42;
  int y = id(x);
};

template<class T>
constexpr int k2(int) {
  return A2(42).y;
}

int
test2 (int i)
{
  return k2<int>(42) + k2<int>(i);
}