Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 2 | |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 3 | template<typename T> |
| 4 | class C { C(int a0 = 0); }; |
| 5 | |
| 6 | template<> |
| 7 | C<char>::C(int a0); |
| 8 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 9 | struct S { }; |
| 10 | |
| 11 | template<typename T> void f1(T a, T b = 10) { } // expected-error{{cannot initialize 'b' with an rvalue of type 'int'}} |
| 12 | |
| 13 | template<typename T> void f2(T a, T b = T()) { } |
| 14 | |
| 15 | template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('struct S' and 'struct S')}} |
| 16 | |
| 17 | void g() { |
| 18 | f1(10); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 19 | f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<struct S>' required here}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 20 | |
| 21 | f2(10); |
| 22 | f2(S()); |
| 23 | |
| 24 | f3(10); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 25 | f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<struct S>' required here}} |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 26 | } |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 27 | |
| 28 | template<typename T> struct F { |
Anders Carlsson | 21e1c4e | 2009-09-06 16:54:02 +0000 | [diff] [blame] | 29 | F(T t = 10); // expected-error{{cannot initialize 't' with an rvalue of type 'int'}} |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 30 | void f(T t = 10); // expected-error{{cannot initialize 't' with an rvalue of type 'int'}} |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Douglas Gregor | 0b84a53 | 2009-08-25 15:24:38 +0000 | [diff] [blame] | 33 | struct FD : F<int> { }; |
| 34 | |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 35 | void g2() { |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 36 | F<int> f; |
Douglas Gregor | 0b84a53 | 2009-08-25 15:24:38 +0000 | [diff] [blame] | 37 | FD fd; |
Anders Carlsson | 8644aec | 2009-08-25 13:07:08 +0000 | [diff] [blame] | 38 | } |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 39 | |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 40 | void g3(F<int> f, F<struct S> s) { |
| 41 | f.f(); |
| 42 | s.f(); // expected-note{{in instantiation of default function argument expression for 'f<struct S>' required here}} |
Anders Carlsson | 21e1c4e | 2009-09-06 16:54:02 +0000 | [diff] [blame] | 43 | |
| 44 | F<int> f2; |
| 45 | F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<struct S>' required here}} |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 48 | template<typename T> struct G { |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 49 | G(T) {} |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | void s(G<int> flags = 10) { } |
| 53 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 54 | // Test default arguments |
| 55 | template<typename T> |
| 56 | struct X0 { |
| 57 | void f(T = T()); // expected-error{{no matching}} |
| 58 | }; |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 60 | template<typename U> |
| 61 | void X0<U>::f(U) { } |
Anders Carlsson | 6bc107b | 2009-09-05 05:38:54 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 63 | void test_x0(X0<int> xi) { |
| 64 | xi.f(); |
| 65 | xi.f(17); |
| 66 | } |
| 67 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 68 | struct NotDefaultConstructible { // expected-note 2{{candidate}} |
| 69 | NotDefaultConstructible(int); // expected-note 2{{candidate}} |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) { |
| 73 | xn.f(NotDefaultConstructible(17)); |
| 74 | xn.f(42); |
| 75 | xn.f(); // expected-note{{in instantiation of default function argument}} |
| 76 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 77 | |
| 78 | template<typename T> |
| 79 | struct X1 { |
| 80 | typedef T value_type; |
| 81 | X1(const value_type& value = value_type()); |
| 82 | }; |
| 83 | |
| 84 | void test_X1() { |
| 85 | X1<int> x1; |
| 86 | } |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 88 | template<typename T> |
| 89 | struct X2 { |
| 90 | void operator()(T = T()); // expected-error{{no matching}} |
| 91 | }; |
| 92 | |
| 93 | void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) { |
| 94 | x2i(); |
| 95 | x2i(17); |
| 96 | x2n(NotDefaultConstructible(17)); |
| 97 | x2n(); // expected-note{{in instantiation of default function argument}} |
| 98 | } |
| 99 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 100 | // PR5283 |
| 101 | namespace PR5283 { |
| 102 | template<typename T> struct A { |
| 103 | A(T = 1); // expected-error 3 {{incompatible type initializing 'int', expected 'int *'}} |
| 104 | }; |
| 105 | |
| 106 | struct B : A<int*> { |
| 107 | B(); |
| 108 | }; |
| 109 | B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 110 | |
| 111 | struct C : virtual A<int*> { |
| 112 | C(); |
| 113 | }; |
| 114 | C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 115 | |
| 116 | struct D { |
| 117 | D(); |
| 118 | |
| 119 | A<int*> a; |
| 120 | }; |
| 121 | D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}} |
| 122 | } |
Sebastian Redl | a29e51b | 2009-11-08 13:56:19 +0000 | [diff] [blame] | 123 | |
| 124 | // PR5301 |
| 125 | namespace pr5301 { |
| 126 | void f(int, int = 0); |
| 127 | |
| 128 | template <typename T> |
| 129 | void g(T, T = 0); |
| 130 | |
| 131 | template <int I> |
| 132 | void i(int a = I); |
| 133 | |
| 134 | template <typename T> |
| 135 | void h(T t) { |
| 136 | f(0); |
| 137 | g(1); |
| 138 | g(t); |
| 139 | i<2>(); |
| 140 | } |
| 141 | |
| 142 | void test() { |
| 143 | h(0); |
| 144 | } |
| 145 | } |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 146 | |