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