Home | History | Annotate | Download | only in SemaTemplate
      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 template<typename T>
      6 class C { C(int a0 = 0); };
      7 
      8 template<>
      9 C<char>::C(int a0);
     10 
     11 struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
     12 #if __cplusplus >= 201103L // C++11 or later
     13 // expected-note@-2 3 {{candidate constructor (the implicit move constructor) not viable}}
     14 #endif
     15 
     16 template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \
     17 // expected-note{{passing argument to parameter 'b' here}}
     18 
     19 template<typename T> void f2(T a, T b = T()) { }
     20 
     21 template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}}
     22 
     23 void g() {
     24   f1(10);
     25   f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}}
     26 
     27   f2(10);
     28   f2(S());
     29 
     30   f3(10);
     31   f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}}
     32 }
     33 
     34 template<typename T> struct F {
     35   F(T t = 10); // expected-error{{no viable conversion}} \
     36   // expected-note{{passing argument to parameter 't' here}}
     37   void f(T t = 10); // expected-error{{no viable conversion}} \
     38   // expected-note{{passing argument to parameter 't' here}}
     39 };
     40 
     41 struct FD : F<int> { };
     42 
     43 void g2() {
     44   F<int> f;
     45   FD fd;
     46 }
     47 
     48 void g3(F<int> f, F<struct S> s) {
     49   f.f();
     50   s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}}
     51 
     52   F<int> f2;
     53   F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}}
     54 }
     55 
     56 template<typename T> struct G {
     57   G(T) {}
     58 };
     59 
     60 void s(G<int> flags = 10) { }
     61 
     62 // Test default arguments
     63 template<typename T>
     64 struct X0 {
     65   void f(T = T()); // expected-error{{no matching}}
     66 };
     67 
     68 template<typename U>
     69 void X0<U>::f(U) { }
     70 
     71 void test_x0(X0<int> xi) {
     72   xi.f();
     73   xi.f(17);
     74 }
     75 
     76 struct NotDefaultConstructible { // expected-note 2 {{candidate constructor (the implicit copy constructor) not viable}}
     77 #if __cplusplus >= 201103L // C++11 or later
     78 // expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}}
     79 #endif
     80   NotDefaultConstructible(int); // expected-note 2{{candidate}}
     81 };
     82 
     83 void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
     84   xn.f(NotDefaultConstructible(17));
     85   xn.f(42);
     86   xn.f(); // expected-note{{in instantiation of default function argument}}
     87 }
     88 
     89 template<typename T>
     90 struct X1 {
     91   typedef T value_type;
     92   X1(const value_type& value = value_type());
     93 };
     94 
     95 void test_X1() {
     96   X1<int> x1;
     97 }
     98 
     99 template<typename T>
    100 struct X2 {
    101   void operator()(T = T()); // expected-error{{no matching}}
    102 };
    103 
    104 void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
    105   x2i();
    106   x2i(17);
    107   x2n(NotDefaultConstructible(17));
    108   x2n(); // expected-note{{in instantiation of default function argument}}
    109 }
    110 
    111 // PR5283
    112 namespace PR5283 {
    113 template<typename T> struct A {
    114   A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}} \
    115   // expected-note 3{{passing argument to parameter here}}
    116 };
    117 
    118 struct B : A<int*> {
    119   B();
    120 };
    121 B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
    122 
    123 struct C : virtual A<int*> {
    124   C();
    125 };
    126 C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
    127 
    128 struct D {
    129   D();
    130 
    131   A<int*> a;
    132 };
    133 D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
    134 }
    135 
    136 // PR5301
    137 namespace pr5301 {
    138   void f(int, int = 0);
    139 
    140   template <typename T>
    141   void g(T, T = 0);
    142 
    143   template <int I>
    144   void i(int a = I);
    145 
    146   template <typename T>
    147   void h(T t) {
    148     f(0);
    149     g(1);
    150     g(t);
    151     i<2>();
    152   }
    153 
    154   void test() {
    155     h(0);
    156   }
    157 }
    158 
    159 // PR5810
    160 namespace PR5810 {
    161   template<typename T>
    162   struct allocator {
    163     allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
    164   };
    165 
    166   template<typename T>
    167   struct vector {
    168     vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
    169   };
    170 
    171   struct A { };
    172   struct B { };
    173 
    174   template<typename>
    175   void FilterVTs() {
    176     vector<A> Result;
    177   }
    178 
    179   void f() {
    180     vector<A> Result;
    181   }
    182 
    183   template<typename T>
    184   struct X {
    185     vector<B> bs;
    186     X() { }
    187   };
    188 
    189   void f2() {
    190     X<float> x; // expected-note{{member function}}
    191   }
    192 }
    193 
    194 template<typename T> void f4(T, int = 17);
    195 template<> void f4<int>(int, int);
    196 
    197 void f4_test(int i) {
    198   f4(i);
    199 }
    200 
    201 // Instantiate for initialization
    202 namespace InstForInit {
    203   template<typename T>
    204   struct Ptr {
    205     typedef T* type;
    206     Ptr(type);
    207   };
    208 
    209   template<typename T>
    210   struct Holder {
    211     Holder(int i, Ptr<T> ptr = 0);
    212   };
    213 
    214   void test_holder(int i) {
    215     Holder<int> h(i);
    216   }
    217 };
    218 
    219 namespace PR5810b {
    220   template<typename T>
    221   T broken() {
    222     T t;
    223     double**** not_it = t;
    224   }
    225 
    226   void f(int = broken<int>());
    227   void g() { f(17); }
    228 }
    229 
    230 namespace PR5810c {
    231   template<typename T>
    232   struct X {
    233     X() {
    234       T t;
    235       double *****p = t; // expected-error{{cannot initialize a variable of type 'double *****' with an lvalue of type 'int'}}
    236     }
    237     X(const X&) { }
    238   };
    239 
    240   struct Y : X<int> { // expected-note{{instantiation of}}
    241   };
    242 
    243   void f(Y y = Y());
    244 
    245   void g() { f(); }
    246 }
    247 
    248 namespace PR8127 {
    249   template< typename T > class PointerClass {
    250   public:
    251     PointerClass( T * object_p ) : p_( object_p ) {
    252       p_->acquire();
    253     }
    254   private:
    255     T * p_;
    256   };
    257 
    258   class ExternallyImplementedClass;
    259 
    260   class MyClass {
    261     void foo( PointerClass<ExternallyImplementedClass> = 0 );
    262   };
    263 }
    264 
    265 namespace rdar8427926 {
    266   template<typename T>
    267   struct Boom {
    268     ~Boom() {
    269       T t;
    270       double *******ptr = t; // expected-error 2{{cannot initialize}}
    271     }
    272   };
    273 
    274   Boom<float> *bfp;
    275 
    276   struct X {
    277     void f(Boom<int> = Boom<int>()) { } // expected-note{{requested here}}
    278     void g(int x = (delete bfp, 0)); // expected-note{{requested here}}
    279   };
    280 
    281   void test(X *x) {
    282     x->f();
    283     x->g();
    284   }
    285 }
    286 
    287 namespace PR8401 {
    288   template<typename T>
    289   struct A {
    290     A() { T* x = 1; } // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
    291   };
    292 
    293   template<typename T>
    294   struct B {
    295     B(const A<T>& a = A<T>()); // expected-note{{in instantiation of}}
    296   };
    297 
    298   void f(B<int> b = B<int>());
    299 
    300   void g() {
    301     f();
    302   }
    303 }
    304 
    305 namespace PR12581 {
    306   const int a = 0;
    307   template < typename > struct A;
    308   template < typename MatrixType, int =
    309   A < MatrixType >::Flags ? : A < MatrixType >::Flags & a > class B;
    310   void
    311   fn1 ()
    312   {
    313   }
    314 }
    315 
    316 namespace PR13758 {
    317   template <typename T> struct move_from {
    318     T invalid;
    319   };
    320   template <class K>
    321   struct unordered_map {
    322     explicit unordered_map(int n = 42);
    323     unordered_map(move_from<K> other);
    324   };
    325   template<typename T>
    326   void StripedHashTable() {
    327     new unordered_map<void>();
    328     new unordered_map<void>;
    329   }
    330   void tt() {
    331     StripedHashTable<int>();
    332   }
    333 }
    334