Debian Codegraph @

clang/test/CXX/temp/temp.constr/temp.constr.normal/p1.cpp

raw
1 // RUN: %clang_cc1 -std=c++2a -x c++ -verify %s
2 // RUN: %clang_cc1 -std=c++2c -x c++ -verify %s
3
4 template<typename T> concept True = true;
5 template<typename T> concept Foo = True<T*>; // #Foo
6 template<typename T> concept Bar = Foo<T&>; // #Bar
7 template<typename T> requires Bar<T> struct S { }; // #S
8 template<typename T> requires Bar<T> && true struct S<T> { }; // #SpecS
9 // expected-error@-1 {{class template partial specialization is not more specialized than the primary template}}
10 // expected-error@#Foo 2{{'type name' declared as a pointer to a reference of type 'T &'}}
11 // expected-note@#SpecS {{while substituting into concept arguments here}}
12 // expected-note@#S {{while substituting into concept arguments here}}
13 // expected-note@#Bar 2{{while substituting into concept arguments here}}
14 // expected-note@#S {{template is declared here}}
15
16
17
18 template<typename T> concept True2 = sizeof(T) >= 0;
19 template<typename T> concept Foo2 = True2<T*>; // #Foo2
20
21 template<typename T> concept Bar2 = Foo2<T&>; // #Bar2
22 // expected-note@-1 3{{while substituting into concept arguments here; substitution failures not allowed in concept arguments}}
23 template<typename T> requires Bar2<T> struct S2 { }; // #SpecS2_1
24 // expected-note@-1{{template is declared here}}
25 template<typename T> requires Bar2<T> && true struct S2<T> { }; // #SpecS2_2
26 // expected-error@-1{{class template partial specialization is not more specialized than the primary template}}
27 // expected-error@#Foo2{{'type name' declared as a pointer to a reference of type 'T &'}}
28
29
30 namespace type_pack {
31 template<typename... Args>
32 concept C1 = ((sizeof(Args) >= 0) && ...);
33
34 template<typename A, typename... B>
35 concept C2 = C1<A, B...>;
36
37 template<typename T>
38 constexpr void foo() requires C2<T, char, T> { }
39
40 template<typename T>
41 constexpr void foo() requires C1<T, char, T> && true { }
42
43 static_assert((foo<int>(), true));
44 }
45
46 namespace template_pack {
47 template<typename T> struct S1 {};
48 template<typename T> struct S2 {};
49
50 template<template<typename> typename... Args>
51 concept C1 = ((sizeof(Args<int>) >= 0) && ...);
52
53 template<template<typename> typename A, template<typename> typename... B>
54 concept C2 = C1<A, B...>;
55
56 template<template<typename> typename T>
57 constexpr void foo() requires C2<T, S1, T> { }
58
59 template<template<typename> typename T>
60 constexpr void foo() requires C1<T, S1, T> && true { }
61
62 static_assert((foo<S2>(), true));
63 }
64
65 namespace non_type_pack {
66 template<int... Args>
67 concept C1 = ((Args >= 0) && ...);
68
69 template<int A, int... B>
70 concept C2 = C1<A, B...>;
71
72 template<int T>
73 constexpr void foo() requires C2<T, 2, T> { }
74
75 template<int T>
76 constexpr void foo() requires C1<T, 2, T> && true { }
77
78 static_assert((foo<1>(), true));
79 }
80
81 namespace PR47174 {
82 // This checks that we don't crash with a failed substitution on the first constrained argument when
83 // performing normalization.
84 template <Bar2 T, True U> // #S3_Header
85 requires true struct S3; // expected-note {{template is declared here}}
86 template <True T, True U>
87 requires true struct S3<T, U>;
88 // expected-error@-1 {{class template partial specialization is not more specialized than the primary template}}
89 // expected-error@#Foo2 2{{'type name' declared as a pointer to a reference of type 'T &'}}
90 // expected-note@#SpecS2_1 {{while substituting into concept arguments here}}
91 // expected-note@#SpecS2_2 {{while substituting into concept arguments here}}
92 // expected-note@#S3_Header {{while substituting into concept arguments here}}
93 // expected-note@#Bar2 {{while substituting into concept arguments here}}
94
95
96 // Same as above, for the second position (but this was already working).
97 template <True T, Bar2 U> // #S4_Header
98 requires true struct S4; // #S4
99 template <True T, True U>
100 requires true struct S4<T, U>; // #S4-spec
101 // expected-error@-1 {{class template partial specialization is not more specialized than the primary template}}
102 // expected-error@#Foo2 {{'type name' declared as a pointer to a reference of type 'U &'}}
103 // expected-note@#S4_Header {{while substituting into concept arguments here}}
104 // expected-note@#S4 {{template is declared here}}
105 // expected-note@#S4 {{similar constraint expressions not considered equivalent}}
106 // expected-note@#S4-spec {{similar constraint expression here}}
107
108
109
110 struct X {
111 template<int> struct Y {
112 using type = int;
113 };
114 };
115
116 template<class T> concept C1 = sizeof(T) != 0;
117 template<class T> concept C2 = C1<typename T::template Y<1>::type>;
118
119 template<class T> requires C1<T> void t1() {}; // expected-note {{candidate function}}
120 template<class T> requires C1<T> && C2<T> void t1() = delete; // expected-note {{candidate function}}
121 template void t1<X>();
122 void t1() { t1<X>(); } // expected-error {{call to deleted function 't1'}}
123
124 template<class T> requires C1<T> void t2() {}; // expected-note 2 {{candidate function}}
125 template<class T> requires C2<T> void t2() {}; // expected-note 2 {{candidate function}}
126 template void t2<X>(); // expected-error {{partial ordering for explicit instantiation of 't2' is ambiguous}}
127 void t2() { t2<X>(); } // expected-error {{call to 't2' is ambiguous}}
128 } // namespace PR47174