1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2
3 void f(int i, int j, int k = 3);
4 void f(int i, int j, int k);
5 void f(int i, int j = 2, int k);
6 void f(int i, int j, int k);
7 void f(int i = 1, int j, int k);
8 void f(int i, int j, int k);
9
i()10 void i()
11 {
12 f();
13 f(0);
14 f(0, 1);
15 f(0, 1, 2);
16 }
17
18
f1(int i,int i,int j)19 int f1(int i, // expected-note {{previous declaration is here}}
20 int i, int j) { // expected-error {{redefinition of parameter 'i'}}
21 i = 17;
22 return j;
23 }
24
25 int x;
26 void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
27
28 void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}}
29
30 class X {
31 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a non-static member function}}
32
g()33 void g() {
34 int f(X* x = this); // expected-error{{default argument references 'this'}}
35 }
36 };
37
38 // C++ [dcl.fct.default]p6
39 class C {
40 static int x;
41 void f(int i = 3); // expected-note{{previous definition is here}}
42 void g(int i, int j = x);
43
44 void h();
45 };
f(int i=3)46 void C::f(int i = 3) // expected-error{{redefinition of default argument}}
47 { }
48
g(int i=88,int j)49 void C::g(int i = 88, int j) {}
50
h()51 void C::h() {
52 g(); // okay
53 }
54
55 // C++ [dcl.fct.default]p9
56 struct Y {
57 int a;
58 int mem1(int i = a); // expected-error{{invalid use of non-static data member 'a'}}
59 int mem2(int i = b); // OK; use Y::b
60 int mem3(int i);
61 int mem4(int i);
62
63 struct Nested {
64 int mem5(int i = b, // OK; use Y::b
65 int j = c, // OK; use Y::Nested::c
66 int k = j, // expected-error{{default argument references parameter 'j'}}
67 int l = a, // expected-error{{invalid use of non-static data member 'a'}}
68 Nested* self = this, // expected-error{{invalid use of 'this' outside of a non-static member function}}
69 int m); // expected-error{{missing default argument on parameter 'm'}}
70 static int c;
71 Nested(int i = 42);
72 };
73
74 int mem7(Nested n = Nested());
75
76 static int b;
77 };
78
mem3(int i=b)79 int Y::mem3(int i = b) { return i; } // OK; use X::b
80
mem4(int i=a)81 int Y::mem4(int i = a) // expected-error{{invalid use of non-static data member 'a'}}
82 { return i; }
83
84
85 // Try to verify that default arguments interact properly with copy
86 // constructors.
87 class Z {
88 public:
89 Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}}
90
f(Z & z)91 void f(Z& z) {
92 Z z2; // expected-error{{no matching constructor for initialization}}
93 Z z3(z);
94 }
95
test_Z(const Z & z)96 void test_Z(const Z& z) {
97 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
98 }
99 };
100
test_Z(const Z & z)101 void test_Z(const Z& z) {
102 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
103 }
104
105 struct ZZ {
106 static ZZ g(int = 17);
107
108 void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} \
109 // expected-note{{passing argument to parameter 'z' here}}
110
111 ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}}
112 };
113
114 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
115 class C2 {
116 static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
117 static int f(int = 10); // expected-note{{default argument declared here}}
118 };
119
120 // Make sure we actually parse the default argument for an inline definition
121 class XX {
A(int length=-1)122 void A(int length = -1 ) { }
B()123 void B() { A(); }
124 };
125
126 template <int I = (1 * I)> struct S {}; // expected-error-re {{use of undeclared identifier 'I'{{$}}}}
127 S<1> s;
128
129 template <int I1 = I2, int I2 = 1> struct T {}; // expected-error-re {{use of undeclared identifier 'I2'{{$}}}}
130 T<0, 1> t;
131