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