default-expr-arguments.cpp revision 220ccbf2c9ef97034cce80561f9f46c4f1f63bc7
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2template<typename T>
3class C { C(int a0 = 0); };
4
5template<>
6C<char>::C(int a0);
7
8struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
9
10template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}}
11
12template<typename T> void f2(T a, T b = T()) { }
13
14template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('struct S' and 'struct S')}}
15
16void g() {
17  f1(10);
18  f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<struct S>' required here}}
19
20  f2(10);
21  f2(S());
22
23  f3(10);
24  f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<struct S>' required here}}
25}
26
27template<typename T> struct F {
28  F(T t = 10); // expected-error{{no viable conversion}}
29  void f(T t = 10); // expected-error{{no viable conversion}}
30};
31
32struct FD : F<int> { };
33
34void g2() {
35  F<int> f;
36  FD fd;
37}
38
39void g3(F<int> f, F<struct S> s) {
40  f.f();
41  s.f(); // expected-note{{in instantiation of default function argument expression for 'f<struct S>' required here}}
42
43  F<int> f2;
44  F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<struct S>' required here}}
45}
46
47template<typename T> struct G {
48  G(T) {}
49};
50
51void s(G<int> flags = 10) { }
52
53// Test default arguments
54template<typename T>
55struct X0 {
56  void f(T = T()); // expected-error{{no matching}}
57};
58
59template<typename U>
60void X0<U>::f(U) { }
61
62void test_x0(X0<int> xi) {
63  xi.f();
64  xi.f(17);
65}
66
67struct NotDefaultConstructible { // expected-note 2{{candidate}}
68  NotDefaultConstructible(int); // expected-note 2{{candidate}}
69};
70
71void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
72  xn.f(NotDefaultConstructible(17));
73  xn.f(42);
74  xn.f(); // expected-note{{in instantiation of default function argument}}
75}
76
77template<typename T>
78struct X1 {
79  typedef T value_type;
80  X1(const value_type& value = value_type());
81};
82
83void test_X1() {
84  X1<int> x1;
85}
86
87template<typename T>
88struct X2 {
89  void operator()(T = T()); // expected-error{{no matching}}
90};
91
92void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
93  x2i();
94  x2i(17);
95  x2n(NotDefaultConstructible(17));
96  x2n(); // expected-note{{in instantiation of default function argument}}
97}
98
99// PR5283
100namespace PR5283 {
101template<typename T> struct A {
102  A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}}
103};
104
105struct B : A<int*> {
106  B();
107};
108B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
109
110struct C : virtual A<int*> {
111  C();
112};
113C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
114
115struct D {
116  D();
117
118  A<int*> a;
119};
120D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
121}
122
123// PR5301
124namespace pr5301 {
125  void f(int, int = 0);
126
127  template <typename T>
128  void g(T, T = 0);
129
130  template <int I>
131  void i(int a = I);
132
133  template <typename T>
134  void h(T t) {
135    f(0);
136    g(1);
137    g(t);
138    i<2>();
139  }
140
141  void test() {
142    h(0);
143  }
144}
145
146// PR5810
147namespace PR5810 {
148  template<typename T>
149  struct allocator {
150    allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array size is negative}}
151  };
152
153  template<typename T>
154  struct vector {
155    vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
156  };
157
158  struct A { };
159  struct B { };
160
161  template<typename>
162  void FilterVTs() {
163    vector<A> Result;
164  }
165
166  void f() {
167    vector<A> Result;
168  }
169
170  template<typename T>
171  struct X {
172    vector<B> bs;
173    X() { }
174  };
175
176  void f2() {
177    X<float> x; // expected-note{{member function}}
178  }
179}
180