default2.cpp revision 61366e9cd41a6dbde4e66416dac21269c8ac1d94
1// RUN: clang -fsyntax-only -verify %s 2void f(int i, int j, int k = 3); 3void f(int i, int j, int k); 4void f(int i, int j = 2, int k); 5void f(int i, int j, int k); 6void f(int i = 1, int j, int k); 7void f(int i, int j, int k); 8 9void i() 10{ 11 f(); 12 f(0); 13 f(0, 1); 14 f(0, 1, 2); 15} 16 17 18int f1(int i, int i, int j) { // expected-error {{redefinition of parameter 'i'}} 19 i = 17; 20 return j; 21} 22 23int x; 24void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}} 25 26void h() 27{ 28 int i; 29 extern void h2(int x = sizeof(i)); // expected-error {{default argument references local variable 'i' of enclosing function}} 30} 31 32void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}} 33 34void nondecl(int (*f)(int x = 5)) // {expected-error {{default arguments can only be specified}}} 35{ 36 void (*f2)(int = 17) // {expected-error {{default arguments can only be specified}}} 37 = (void (*)(int = 42))f; // {expected-error {{default arguments can only be specified}}} 38} 39 40class X { 41 void f(X* x = this); // expected-error{{invalid use of 'this' outside of a nonstatic member function}} 42 43 void g() { 44 int f(X* x = this); // expected-error{{default argument references 'this'}} 45 } 46}; 47 48// C++ [dcl.fct.default]p6 49class C { 50 static int x; 51 void f(int i = 3); // expected-note{{previous definition is here}} 52 void g(int i, int j = x); 53 54 void h(); 55}; 56void C::f(int i = 3) // expected-error{{redefinition of default argument}} 57{ } 58 59void C::g(int i = 88, int j) {} 60 61void C::h() { 62 g(); // okay 63} 64 65// C++ [dcl.fct.default]p9 66struct Y { 67 int a; 68 int mem1(int i = a); // expected-error{{invalid use of nonstatic data member 'a'}} 69 int mem2(int i = b); // OK; use Y::b 70 int mem3(int i); 71 int mem4(int i); 72 73 struct Nested { 74 int mem5(int i = b, // OK; use Y::b 75 int j = c, // OK; use Y::Nested::c 76 int k = j, // expected-error{{default argument references parameter 'j'}} 77 int l = a, // expected-error{{invalid use of nonstatic data member 'a'}} 78 Nested* self = this, // expected-error{{invalid use of 'this' outside of a nonstatic member function}} 79 int m); // expected-error{{missing default argument on parameter 'm'}} 80 static int c; 81 }; 82 83 static int b; 84 85 int (*f)(int = 17); // expected-error{{default arguments can only be specified for parameters in a function declaration}} 86 87 void mem8(int (*fp)(int) = (int (*)(int = 17))0); // expected-error{{default arguments can only be specified for parameters in a function declaration}} 88}; 89 90int Y::mem3(int i = b) { return i; } // OK; use X::b 91 92int Y::mem4(int i = a) // expected-error{{invalid use of nonstatic data member 'a'}} 93{ return i; } 94 95 96// Try to verify that default arguments interact properly with copy 97// constructors. 98class Z { 99public: 100 Z(Z&, int i = 17); // expected-note{{candidate function}} 101 102 void f(Z& z) { 103 Z z2; // expected-error{{no matching constructor for initialization}} 104 Z z3(z); 105 } 106 107 void test_Z(const Z& z) { 108 Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}} 109 } 110}; 111 112void test_Z(const Z& z) { 113 Z z2(z); // expected-error{{no matching constructor for initialization of 'z2'}} 114} 115 116struct ZZ { 117 void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} 118 119 static ZZ g(int = 17); 120 121 ZZ(ZZ&, int = 17); // expected-note{{candidate function}} 122}; 123