blob: 6391369aa5c470ece2245470d6f5dd69d609607f [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:241// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregorf8f86832009-02-11 18:16:402template<typename T, int N = 2> struct X; // expected-note{{template is declared here}}
3
4X<int, 1> *x1;
5X<int> *x2;
6
Douglas Gregor7f741122009-02-25 19:37:187X<> *x3; // expected-error{{too few template arguments for class template 'X'}}
Douglas Gregorf8f86832009-02-11 18:16:408
9template<typename U = float, int M> struct X;
10
11X<> *x4;
Anders Carlssondd096d82009-06-05 02:12:3212
Anders Carlsson03c9e872009-06-05 02:45:2413template<typename T = int> struct Z { };
Anders Carlssondd096d82009-06-05 02:12:3214template struct Z<>;
Anders Carlsson40ed3442009-06-11 16:06:4915
16// PR4362
17template<class T> struct a { };
18template<> struct a<int> { static const bool v = true; };
19
20template<class T, bool = a<T>::v> struct p { }; // expected-error {{no member named 'v'}}
21
22template struct p<bool>; // expected-note {{in instantiation of default argument for 'p<bool>' required here}}
23template struct p<int>;
Douglas Gregor568a0712009-10-14 17:30:5824
25// PR5187
26template<typename T, typename U>
27struct A;
28
29template<typename T, typename U = T>
30struct A;
31
32template<typename T, typename U>
33struct A {
34 void f(A<T>);
35};
36
37template<typename T>
38struct B { };
39
40template<>
41struct B<void> {
42 typedef B<void*> type;
43};
Douglas Gregor36d7c5f2009-11-09 19:17:5044
45// Nested default arguments for template parameters.
46template<typename T> struct X1 { };
47
48template<typename T>
49struct X2 {
50 template<typename U = typename X1<T>::type> // expected-error{{no type named}}
51 struct Inner1 { };
52
53 template<T Value = X1<T>::value> // expected-error{{no member named 'value'}}
54 struct NonType1 { };
55
56 template<T Value>
57 struct Inner2 { };
58
59 template<typename U>
60 struct Inner3 {
61 template<typename X = T, typename V = U>
62 struct VeryInner { };
63
64 template<T Value1 = sizeof(T), T Value2 = sizeof(U),
65 T Value3 = Value1 + Value2>
66 struct NonType2 { };
67 };
68};
69
70X2<int> x2i;
71X2<int>::Inner1<float> x2iif;
72
73X2<int>::Inner1<> x2bad; // expected-note{{instantiation of default argument}}
74
75X2<int>::NonType1<'a'> x2_nontype1;
76X2<int>::NonType1<> x2_nontype1_bad; // expected-note{{instantiation of default argument}}
77
78// Check multi-level substitution into template type arguments
79X2<int>::Inner3<float>::VeryInner<> vi;
80X2<char>::Inner3<int>::NonType2<> x2_deep_nontype;
81
Douglas Gregor36d7c5f2009-11-09 19:17:5082template<typename T, typename U>
83struct is_same { static const bool value = false; };
84
85template<typename T>
86struct is_same<T, T> { static const bool value = true; };
87
Douglas Gregor120635b2009-11-11 16:39:3488int array1[is_same<__typeof__(vi),
Douglas Gregor36d7c5f2009-11-09 19:17:5089 X2<int>::Inner3<float>::VeryInner<int, float> >::value? 1 : -1];
90
Douglas Gregor120635b2009-11-11 16:39:3491int array2[is_same<__typeof(x2_deep_nontype),
92 X2<char>::Inner3<int>::NonType2<sizeof(char), sizeof(int),
Douglas Gregor36d7c5f2009-11-09 19:17:5093 sizeof(char)+sizeof(int)> >::value? 1 : -1];
Douglas Gregor120635b2009-11-11 16:39:3494
95// Template template parameter defaults
96template<template<typename T> class X = X2> struct X3 { };
97int array3[is_same<X3<>, X3<X2> >::value? 1 : -1];
98
99struct add_pointer {
100 template<typename T>
101 struct apply {
102 typedef T* type;
103 };
104};
105
106template<typename T, template<typename> class X = T::template apply>
107 struct X4;
108int array4[is_same<X4<add_pointer>,
109 X4<add_pointer, add_pointer::apply> >::value? 1 : -1];
Douglas Gregore62e6a02009-11-11 19:13:48110
111template<int> struct X5 {}; // expected-note{{has a different type 'int'}}
112template<long> struct X5b {};
113template<typename T,
114 template<T> class B = X5> // expected-error{{template template argument has different}} \
115 // expected-note{{previous non-type template parameter}}
116 struct X6 {};
117
118X6<int> x6a;
Douglas Gregor84d49a22009-11-11 21:54:23119X6<long> x6b; // expected-note{{while checking a default template argument}}
Douglas Gregore62e6a02009-11-11 19:13:48120X6<long, X5b> x6c;
Douglas Gregorc998409c2009-11-12 00:03:40121
122
123template<template<class> class X = B<int> > struct X7; // expected-error{{must be a class template}}
Douglas Gregor55462622011-06-15 14:20:42124
125namespace PR9643 {
126 template<typename T> class allocator {};
127 template<typename T, typename U = allocator<T> > class vector {};
128
129 template<template<typename U, typename = allocator<U> > class container,
130 typename DT>
131 container<DT> initializer(const DT& d) {
132 return container<DT>();
133 }
134
135 void f() {
136 vector<int, allocator<int> > v = initializer<vector>(5);
137 }
138}