default-expr-arguments.cpp revision a29e51bb9874bb9ce442efa271e87da237e4ce2c
1// RUN: clang-cc -fsyntax-only -verify %s
2
3template<typename T>
4class C { C(int a0 = 0); };
5
6template<>
7C<char>::C(int a0);
8
9struct S { };
10
11template<typename T> void f1(T a, T b = 10) { } // expected-error{{cannot initialize 'b' with an rvalue of type 'int'}}
12
13template<typename T> void f2(T a, T b = T()) { }
14
15template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('struct S' and 'struct S')}}
16
17void g() {
18  f1(10);
19  f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<struct 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<struct S>' required here}}
26}
27
28template<typename T> struct F {
29  F(T t = 10); // expected-error{{cannot initialize 't' with an rvalue of type 'int'}}
30  void f(T t = 10); // expected-error{{cannot initialize 't' with an rvalue of type 'int'}}
31};
32
33struct FD : F<int> { };
34
35void g2() {
36  F<int> f;
37  FD fd;
38}
39
40void g3(F<int> f, F<struct S> s) {
41  f.f();
42  s.f(); // expected-note{{in instantiation of default function argument expression for 'f<struct S>' required here}}
43
44  F<int> f2;
45  F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<struct S>' required here}}
46}
47
48template<typename T> struct G {
49  G(T) {}
50};
51
52void s(G<int> flags = 10) { }
53
54// Test default arguments
55template<typename T>
56struct X0 {
57  void f(T = T()); // expected-error{{no matching}}
58};
59
60template<typename U>
61void X0<U>::f(U) { }
62
63void test_x0(X0<int> xi) {
64  xi.f();
65  xi.f(17);
66}
67
68struct NotDefaultConstructible { // expected-note{{candidate}}
69  NotDefaultConstructible(int); // expected-note{{candidate}}
70};
71
72void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
73  xn.f(NotDefaultConstructible(17));
74  xn.f(42);
75  xn.f(); // expected-note{{in instantiation of default function argument}}
76}
77
78template<typename T>
79struct X1 {
80  typedef T value_type;
81  X1(const value_type& value = value_type());
82};
83
84void test_X1() {
85  X1<int> x1;
86}
87
88// PR5283
89namespace PR5283 {
90template<typename T> struct A {
91  A(T = 1); // expected-error 3 {{incompatible type initializing 'int', expected 'int *'}}
92};
93
94struct B : A<int*> {
95  B();
96};
97B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
98
99struct C : virtual A<int*> {
100  C();
101};
102C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
103
104struct D {
105  D();
106
107  A<int*> a;
108};
109D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
110}
111
112// PR5301
113namespace pr5301 {
114  void f(int, int = 0);
115
116  template <typename T>
117  void g(T, T = 0);
118
119  template <int I>
120  void i(int a = I);
121
122  template <typename T>
123  void h(T t) {
124    f(0);
125    g(1);
126    g(t);
127    i<2>();
128  }
129
130  void test() {
131    h(0);
132  }
133}
134