p7.cpp
5.78 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 -std=c++98 %s -Wno-c++11-extensions
// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 -std=c++17 %s
// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx20 -std=c++20 %s
// C++98:
// A non-type template-parameter shall not be declared to have
// floating point, class, or void type.
struct A; // expected-note {{forward declaration}}
template<double d> class X; // cxx17-error{{cannot have type}}
template<double* pd> class Y; //OK
template<double& rd> class Z; //OK
template<A a> class X0; // expected-error{{has incomplete type 'A'}}
struct A {};
template<A a> class X0b; // cxx17-error{{cannot have type 'A' before C++20}}
typedef void VOID;
template<VOID a> class X01; // expected-error{{has incomplete type 'VOID'}}
// C++11 disallows rvalue references.
template<int &R> struct lval_ref;
template<int &&R> struct rval_ref; // expected-warning 0-1{{extension}} expected-error {{non-type template parameter has rvalue reference type 'int &&'}}
// C++20 requires a structural type. In addition to the above cases, this allows:
// arbitrary scalar types; we generally include complex types in that list
template<_Complex float ci> struct ComplexFloat; // cxx17-error {{cannot have type '_Complex float' before C++20}}
template<_Complex int ci> struct ComplexInt; // cxx17-error {{cannot have type '_Complex int' before C++20}}
template<_ExtInt(42) ei> struct ExtInt;
// atomic types aren't scalar types
template<_Atomic float ci> struct AtomicFloat; // expected-error {{cannot have type '_Atomic(float)'}}
template<_Atomic int ci> struct AtomicInt; // expected-error {{cannot have type '_Atomic(int)'}}
// we allow vector types as an extension
typedef __attribute__((ext_vector_type(4))) int VI4;
typedef __attribute__((ext_vector_type(4))) float VF4;
template<VI4> struct VectorInt; // cxx17-error {{cannot have type 'VI4'}}
template<VF4> struct VectorFloat; // cxx17-error {{cannot have type 'VF4'}}
struct A2 {};
struct RRef {
int &&r; // cxx20-note 1+{{'RRef' is not a structural type because it has a non-static data member of rvalue reference type}}
};
// class types with all public members and bases, no mutable state, and no rvalue references.
struct B : A, public A2 {
int a;
private:
void f();
static int s;
public:
float g;
int &r = a;
void *p;
A2 a2;
RRef *ptr_to_bad;
RRef &ref_to_bad = *ptr_to_bad;
_Complex int ci;
_Complex float cf;
_ExtInt(42) ei;
VI4 vi4;
VF4 vf4;
};
template<B> struct ClassNTTP {}; // cxx17-error {{cannot have type 'B'}}
template<RRef> struct WithRRef {}; // cxx17-error {{cannot have type 'RRef'}}
// cxx20-error@-1 {{type 'RRef' of non-type template parameter is not a structural type}}
struct BadBase
: RRef {}; // cxx20-note {{'BadBase' is not a structural type because it has a base class of non-structural type 'RRef'}}
template<BadBase> struct WithBadBase {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct BadField {
RRef r; // cxx20-note {{'BadField' is not a structural type because it has a non-static data member of non-structural type 'RRef'}}
};
template<BadField> struct WithBadField {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct BadFieldArray {
RRef r[3][2]; // cxx20-note {{'BadFieldArray' is not a structural type because it has a non-static data member of non-structural type 'RRef'}}
};
template<BadFieldArray> struct WithBadFieldArray {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct ProtectedBase
: protected A {}; // cxx20-note {{'ProtectedBase' is not a structural type because it has a base class that is not public}}
template<ProtectedBase> struct WithProtectedBase {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct PrivateBase
: private A {}; // cxx20-note {{'PrivateBase' is not a structural type because it has a base class that is not public}}
template<PrivateBase> struct WithPrivateBase {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
class Private2Base
: A {}; // cxx20-note {{'Private2Base' is not a structural type because it has a base class that is not public}}
template<Private2Base> struct WithPrivate2Base {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct ProtectedField {
protected:
A r; // cxx20-note {{'ProtectedField' is not a structural type because it has a non-static data member that is not public}}
};
template<ProtectedField> struct WithProtectedField {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct PrivateField {
private:
A r; // cxx20-note {{'PrivateField' is not a structural type because it has a non-static data member that is not public}}
};
template<PrivateField> struct WithPrivateField {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
class Private2Field {
A r; // cxx20-note {{'Private2Field' is not a structural type because it has a non-static data member that is not public}}
};
template<Private2Field> struct WithPrivate2Field {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
struct MutableField {
mutable int n; // cxx20-note {{'MutableField' is not a structural type because it has a mutable non-static data member}}
};
template<MutableField> struct WithMutableField {}; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
template<typename T> struct BadExtType { T t; }; // cxx20-note 2{{has a non-static data member of non-structural type}}
template<BadExtType<_Atomic float> > struct AtomicFloatField; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}
template<BadExtType<_Atomic int> > struct AtomicInt; // cxx17-error {{cannot have type}} cxx20-error {{is not a structural type}}