145 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx17 %std_cxx11-14 %s2// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 %std_cxx17- %s3 4void f(int i, int j, int k = 3);5void f(int i, int j, int k);6void f(int i, int j = 2, int k);7void f(int i, int j, int k);8void f(int i = 1, int j, int k);9void f(int i, int j, int k);10 11void i()12{13 f();14 f(0);15 f(0, 1);16 f(0, 1, 2);17}18 19 20int 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 26int x;27void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}28 29void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}}30 31class X {32 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a non-static member function}}33 34 void g() { 35 int f(X* x = this); // expected-error{{default argument references 'this'}}36 }37};38 39// C++ [dcl.fct.default]p640class 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}; 47void C::f(int i = 3) // expected-error{{redefinition of default argument}}48{ } 49 50void C::g(int i = 88, int j) {}51 52void C::h() {53 g(); // okay54}55 56// C++ [dcl.fct.default]p957struct 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::b66 int j = c, // OK; use Y::Nested::c67 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 80int Y::mem3(int i = b) { return i; } // OK; use X::b81 82int 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 copy87// constructors.88class Z {89public:90 Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}}91 92 void f(Z& z) { 93 Z z2; // expected-error{{no matching constructor for initialization}}94 Z z3(z);95 }96 97 void test_Z(const Z& z) {98 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}99 }100};101 102void test_Z(const Z& z) {103 Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}104}105 106struct 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#325116class 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 121template <typename T> class C3;122template <> 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 definition128class XX {129 void A(int length = -1 ) { } 130 void B() { A(); }131};132 133template <int I = (1 * I)> struct S {}; // expected-error-re {{use of undeclared identifier 'I'{{$}}}}134S<1> s;135 136template <int I1 = I2, int I2 = 1> struct T {}; // expected-error-re {{use of undeclared identifier 'I2'{{$}}}}137T<0, 1> t;138 139struct 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