Debian Codegraph @

gcc/testsuite/g++.dg/DRs/dr2478.C

raw
1 // DR 2478 - Properties of explicit specializations of implicitly-instantiated class templates
2 // { dg-do compile { target c++20 } }
3
4 // Defeat -fimplicit-constexpr
5 int ii; // { dg-prune-output "value of 'ii' is not usable in a constant expr" }
6
7 template <typename T>
8 struct S {
9 int foo () { return ii; }
10 constexpr int bar () { return 0; }
11 int baz () { return ii; }
12 consteval int qux () { return 0; }
13 constexpr S () {}
14 static constinit T x;
15 static T y;
16 };
17
18 template <typename T>
19 T S<T>::x = S<T> ().foo (); // { dg-error "'constinit' variable 'S<char>::x' does not have a constant initializer" }
20 // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 }
21
22 template <typename T>
23 T S<T>::y = S<T> ().foo ();
24
25 template <>
26 constexpr int
27 S<int>::foo ()
28 {
29 return 0;
30 }
31
32 template <>
33 int
34 S<int>::bar ()
35 {
36 return 0;
37 }
38
39 template <>
40 consteval int
41 S<char>::baz ()
42 {
43 return 0;
44 }
45
46 template <>
47 int
48 S<char>::qux ()
49 {
50 return 0;
51 }
52
53 template <>
54 long S<long>::x = S<long> ().foo (); // { dg-bogus "'constinit' variable 'S<long int>::x' does not have a constant initializer" "" { xfail *-*-* } }
55 // { dg-bogus "call to non-'constexpr' function|called in a constant expression" "" { xfail *-*-* } .-1 }
56
57 template <>
58 constinit long S<long>::y = S<long> ().foo (); // { dg-error "'constinit' variable 'S<long int>::y' does not have a constant initializer" }
59 // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 }
60
61 constinit auto a = S<char> ().foo (); // { dg-error "'constinit' variable 'a' does not have a constant initializer" }
62 // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 }
63 constinit auto b = S<char> ().bar ();
64 constinit auto c = S<int> ().foo ();
65 constinit auto d = S<int> ().bar (); // { dg-error "'constinit' variable 'd' does not have a constant initializer" }
66 // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 }
67 constinit auto e = S<char> ().baz ();
68 constinit auto f = S<char> ().qux (); // { dg-error "'constinit' variable 'f' does not have a constant initializer" }
69 // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 }
70 constinit auto g = S<int> ().baz (); // { dg-error "'constinit' variable 'g' does not have a constant initializer" }
71 // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 }
72 constinit auto h = S<int> ().qux ();
73 auto i = S<char>::x;
74 auto j = S<int>::x;
75 auto k = S<long>::x;
76 auto l = S<char>::y;
77 auto m = S<int>::y;