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