xref: /llvm-project/clang/test/SemaCXX/default2.cpp (revision dd1cb5b2ec0b6bd91bb1d9dd0fd6bb20422d9451)
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 
10 void i()
11 {
12   f();
13   f(0);
14   f(0, 1);
15   f(0, 1, 2);
16 }
17 
18 
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 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
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 };
46 void C::f(int i = 3) // expected-error{{redefinition of default argument}}
47 { }
48 
49 void C::g(int i = 88, int j) {}
50 
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 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 
76 int Y::mem3(int i = b) { return i; } // OK; use X::b
77 
78 int 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.
84 class Z {
85 public:
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 'class Z'}}
95   }
96 };
97 
98 void test_Z(const Z& z) {
99   Z z2(z); // expected-error{{no matching constructor for initialization of 'class Z'}}
100 }
101 
102 struct ZZ {
103   static ZZ g(int = 17);
104 
105   void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}}
106 
107   ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}}
108 };
109 
110 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
111 class C2 {
112   static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
113   static int f(int = 10); // expected-note{{default argument declared here}}
114 };
115 
116 // Make sure we actually parse the default argument for an inline definition
117 class XX {
118   void A(int length = -1 ) {  }
119   void B() { A(); }
120 };
121