default-expr-arguments.cpp revision d47c47d65e339ab1f28e9f9365159cf1047ac1df
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 2{{candidate}}
69  NotDefaultConstructible(int); // expected-note 2{{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
88template<typename T>
89struct X2 {
90  void operator()(T = T()); // expected-error{{no matching}}
91};
92
93void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
94  x2i();
95  x2i(17);
96  x2n(NotDefaultConstructible(17));
97  x2n(); // expected-note{{in instantiation of default function argument}}
98}
99
100// PR5283
101namespace PR5283 {
102template<typename T> struct A {
103  A(T = 1); // expected-error 3 {{incompatible type initializing 'int', expected 'int *'}}
104};
105
106struct B : A<int*> {
107  B();
108};
109B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
110
111struct C : virtual A<int*> {
112  C();
113};
114C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
115
116struct D {
117  D();
118
119  A<int*> a;
120};
121D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
122}
123
124// PR5301
125namespace pr5301 {
126  void f(int, int = 0);
127
128  template <typename T>
129  void g(T, T = 0);
130
131  template <int I>
132  void i(int a = I);
133
134  template <typename T>
135  void h(T t) {
136    f(0);
137    g(1);
138    g(t);
139    i<2>();
140  }
141
142  void test() {
143    h(0);
144  }
145}
146
147