default2.cpp revision a41a8c5972c2632247ae7913cf6ce65d45f7e702
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,          // 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
25int x;
26void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
27
28void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}}
29
30class X {
31  void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
32
33  void g() {
34    int f(X* x = this); // expected-error{{default argument references 'this'}}
35  }
36};
37
38// C++ [dcl.fct.default]p6
39class 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};
46void C::f(int i = 3) // expected-error{{redefinition of default argument}}
47{ }
48
49void C::g(int i = 88, int j) {}
50
51void C::h() {
52  g(); // okay
53}
54
55// C++ [dcl.fct.default]p9
56struct Y {
57  int a;
58  int mem1(int i = a); // expected-error{{invalid use of nonstatic 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 nonstatic data member 'a'}}
68             Nested* self = this, // expected-error{{invalid use of 'this' outside of a nonstatic member function}}
69             int m); // expected-error{{missing default argument on parameter 'm'}}
70    static int c;
71  };
72
73  static int b;
74};
75
76int Y::mem3(int i = b) { return i; } // OK; use X::b
77
78int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}}
79{ return i; }
80
81
82// Try to verify that default arguments interact properly with copy
83// constructors.
84class Z {
85public:
86  Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}}
87
88  void f(Z& z) {
89    Z z2;    // expected-error{{no matching constructor for initialization}}
90    Z z3(z);
91  }
92
93  void test_Z(const Z& z) {
94    Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
95  }
96};
97
98void test_Z(const Z& z) {
99  Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
100}
101
102struct ZZ {
103  static ZZ g(int = 17);
104
105  void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} \
106  // expected-note{{passing argument to parameter 'z' here}}
107
108  ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}}
109};
110
111// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
112class C2 {
113  static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
114  static int f(int = 10); // expected-note{{default argument declared here}}
115};
116
117// Make sure we actually parse the default argument for an inline definition
118class XX {
119  void A(int length = -1 ) {  }
120  void B() { A(); }
121};
122