[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / default2.cpp
blob355a96bbcb99f8e6c4b412e82d7928ba9c9577cf
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
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);
11 void i()
13 f();
14 f(0);
15 f(0, 1);
16 f(0, 1, 2);
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;
26 int x;
27 void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
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'}}
31 class X {
32 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a non-static member function}}
34 void g() {
35 int f(X* x = this); // expected-error{{default argument references 'this'}}
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);
45 void h();
46 };
47 void C::f(int i = 3) // expected-error{{redefinition of default argument}}
48 { }
50 void C::g(int i = 88, int j) {}
52 void C::h() {
53 g(); // okay
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);
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);
75 int mem7(Nested n = Nested());
77 static int b;
78 };
80 int Y::mem3(int i = b) { return i; } // OK; use X::b
82 int Y::mem4(int i = a) // expected-error{{invalid use of non-static data member 'a'}}
83 { return i; }
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}}
92 void f(Z& z) {
93 Z z2; // expected-error{{no matching constructor for initialization}}
94 Z z3(z);
97 void test_Z(const Z& z) {
98 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
102 void test_Z(const Z& z) {
103 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
106 struct ZZ {
107 static ZZ g(int = 17);
109 void f(ZZ z = g()); // precxx17-error{{no matching constructor for initialization}} \
110 // precxx17-note{{passing argument to parameter 'z' here}}
112 ZZ(ZZ&, int = 17); // precxx17-note{{candidate constructor}}
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}}
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}}
127 // Make sure we actually parse the default argument for an inline definition
128 class XX {
129 void A(int length = -1 ) { }
130 void B() { A(); }
133 template <int I = (1 * I)> struct S {}; // expected-error-re {{use of undeclared identifier 'I'{{$}}}}
134 S<1> s;
136 template <int I1 = I2, int I2 = 1> struct T {}; // expected-error-re {{use of undeclared identifier 'I2'{{$}}}}
137 T<0, 1> t;
139 struct PR28105 {
140 PR28105 (int = 0, int = 0,
141 PR28105 // expected-error{{recursive evaluation of default argument}}
143 0); // expected-note {{default argument used here}}