xref: /llvm-project/clang/test/SemaTemplate/partial-spec-instantiate.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4 
5 // PR4607
6 template <class T> struct X {};
7 
8 template <> struct X<char>
9 {
10   static char* g();
11 };
12 
13 template <class T> struct X2 {};
14 
15 template <class U>
16 struct X2<U*> {
fX217   static void f() {
18     X<U>::g();
19   }
20 };
21 
a(char * a,char * b)22 void a(char *a, char *b) {X2<char*>::f();}
23 
24 namespace WonkyAccess {
25   template<typename T>
26   struct X {
27     int m;
28   };
29 
30   template<typename U>
31   class Y;
32 
33   template<typename U>
34   struct Y<U*> : X<U> { };
35 
36   template<>
37   struct Y<float*> : X<float> { };
38 
f(Y<int * > y,Y<float * > y2)39   int f(Y<int*> y, Y<float*> y2) {
40     return y.m + y2.m;
41   }
42 }
43 
44 namespace rdar9169404 {
45   template<typename T, T N> struct X { };
46   template<bool C> struct X<bool, C> {
47     typedef int type;
48   };
49 
50   X<bool, -1>::type value;
51 #if __cplusplus >= 201103L
52   // expected-error@-2 {{non-type template argument evaluates to -1, which cannot be narrowed to type 'bool'}}
53 #endif
54 }
55 
56 namespace rdar39524996 {
57   template <typename T, typename U>
58   struct enable_if_not_same
59   {
60     typedef void type;
61   };
62   template <typename T>
63   struct enable_if_not_same<T, T>;
64 
65   template <typename T>
66   struct Wrapper {
67     // Assertion triggered on trying to set twice the same partial specialization
68     // enable_if_not_same<int, int>
69     template <class U>
Wrapperrdar39524996::Wrapper70     Wrapper(const Wrapper<U>& other,
71             typename enable_if_not_same<U, T>::type* = 0) {}
72 
Wrapperrdar39524996::Wrapper73     explicit Wrapper(int i) {}
74   };
75 
76   template <class T>
77   struct Container {
78     // It is important that the struct has implicit copy and move constructors.
Containerrdar39524996::Container79     Container() : x() {}
80 
81     template <class U>
Containerrdar39524996::Container82     Container(const Container<U>& other) : x(static_cast<T>(other.x)) {}
83 
84     // Implicit constructors are member-wise, so the field triggers instantiation
85     // of T constructors and we instantiate all of them for overloading purposes.
86     T x;
87   };
88 
89   void takesWrapperInContainer(const Container< Wrapper<int> >& c);
test()90   void test() {
91     // Type mismatch triggers initialization with conversion which requires
92     // implicit constructors to be instantiated.
93     Container<int> c;
94     takesWrapperInContainer(c);
95   }
96 }
97 
98 namespace InstantiationDependent {
99   template<typename> using ignore = void; // expected-warning 0-1{{extension}}
100   template<typename T, typename = void> struct A {
101     static const bool specialized = false;
102   };
103   template<typename T> struct Hide { typedef void type; };
104   template<typename T> struct A<T, Hide<ignore<typename T::type> >::type> {
105     static const bool specialized = true;
106   };
107 
108   struct X {};
109   struct Y { typedef int type; };
110   _Static_assert(!A<X>::specialized, "");
111   _Static_assert(A<Y>::specialized, "");
112 }
113 
114 namespace IgnorePartialSubstitution {
115   template <typename... T> struct tuple {}; // expected-warning 0-1{{extension}}
116   template <typename> struct IsTuple {
117     enum { value = false };
118   };
119   template <typename... Us> struct IsTuple<tuple<Us...> > { // expected-warning 0-1{{extension}}
120     enum { value = true };
121   };
122 
123   template <bool...> using ignore = void; // expected-warning 0-2{{extension}}
124   template <class... Pred> ignore<Pred::value...> helper(); // expected-warning 0-1{{extension}}
125 
126   using S = IsTuple<tuple<int> >; // expected-warning 0-1{{extension}}
127 
128   // This used to pick the primary template, because we got confused and
129   // thought that template parameter 0 was the current partially-substituted
130   // pack (from `helper`) during the deduction for the partial specialization.
f()131   void f() { helper<S>(); }
132 
133   _Static_assert(S::value, "");
134 }
135 
136 namespace GH60778 {
137   template <bool B = false> class ClassTemplate {
138   public:
139       template <typename T, typename = void> class Nested {};
140   };
141 
142   template <typename DerivedType> class Base {};
143 
144   template <>
145   template <typename T>
146   class ClassTemplate<>::Nested<T> : public Base<ClassTemplate<>::Nested<T> > {};
147 
use()148   void use() {
149     // This should instantiate the body of Nested with the template arguments
150     // from the Partial Specialization. This would previously get confused and
151     // get the template arguments from the primary template instead.
152     ClassTemplate<>::Nested<int> instantiation;
153   }
154 }
155