default-expr-arguments.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template<typename T>
  3. class C { C(int a0 = 0); };
  4. template<>
  5. C<char>::C(int a0);
  6. struct S { }; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
  7. template<typename T> void f1(T a, T b = 10) { } // expected-error{{no viable conversion}} \
  8. // expected-note{{passing argument to parameter 'b' here}}
  9. template<typename T> void f2(T a, T b = T()) { }
  10. template<typename T> void f3(T a, T b = T() + T()); // expected-error{{invalid operands to binary expression ('S' and 'S')}}
  11. void g() {
  12. f1(10);
  13. f1(S()); // expected-note{{in instantiation of default function argument expression for 'f1<S>' required here}}
  14. f2(10);
  15. f2(S());
  16. f3(10);
  17. f3(S()); // expected-note{{in instantiation of default function argument expression for 'f3<S>' required here}}
  18. }
  19. template<typename T> struct F {
  20. F(T t = 10); // expected-error{{no viable conversion}} \
  21. // expected-note{{passing argument to parameter 't' here}}
  22. void f(T t = 10); // expected-error{{no viable conversion}} \
  23. // expected-note{{passing argument to parameter 't' here}}
  24. };
  25. struct FD : F<int> { };
  26. void g2() {
  27. F<int> f;
  28. FD fd;
  29. }
  30. void g3(F<int> f, F<struct S> s) {
  31. f.f();
  32. s.f(); // expected-note{{in instantiation of default function argument expression for 'f<S>' required here}}
  33. F<int> f2;
  34. F<S> s2; // expected-note{{in instantiation of default function argument expression for 'F<S>' required here}}
  35. }
  36. template<typename T> struct G {
  37. G(T) {}
  38. };
  39. void s(G<int> flags = 10) { }
  40. // Test default arguments
  41. template<typename T>
  42. struct X0 {
  43. void f(T = T()); // expected-error{{no matching}}
  44. };
  45. template<typename U>
  46. void X0<U>::f(U) { }
  47. void test_x0(X0<int> xi) {
  48. xi.f();
  49. xi.f(17);
  50. }
  51. struct NotDefaultConstructible { // expected-note 2{{candidate}}
  52. NotDefaultConstructible(int); // expected-note 2{{candidate}}
  53. };
  54. void test_x0_not_default_constructible(X0<NotDefaultConstructible> xn) {
  55. xn.f(NotDefaultConstructible(17));
  56. xn.f(42);
  57. xn.f(); // expected-note{{in instantiation of default function argument}}
  58. }
  59. template<typename T>
  60. struct X1 {
  61. typedef T value_type;
  62. X1(const value_type& value = value_type());
  63. };
  64. void test_X1() {
  65. X1<int> x1;
  66. }
  67. template<typename T>
  68. struct X2 {
  69. void operator()(T = T()); // expected-error{{no matching}}
  70. };
  71. void test_x2(X2<int> x2i, X2<NotDefaultConstructible> x2n) {
  72. x2i();
  73. x2i(17);
  74. x2n(NotDefaultConstructible(17));
  75. x2n(); // expected-note{{in instantiation of default function argument}}
  76. }
  77. // PR5283
  78. namespace PR5283 {
  79. template<typename T> struct A {
  80. A(T = 1); // expected-error 3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'int'}} \
  81. // expected-note 3{{passing argument to parameter here}}
  82. };
  83. struct B : A<int*> {
  84. B();
  85. };
  86. B::B() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
  87. struct C : virtual A<int*> {
  88. C();
  89. };
  90. C::C() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
  91. struct D {
  92. D();
  93. A<int*> a;
  94. };
  95. D::D() { } // expected-note {{in instantiation of default function argument expression for 'A<int *>' required he}}
  96. }
  97. // PR5301
  98. namespace pr5301 {
  99. void f(int, int = 0);
  100. template <typename T>
  101. void g(T, T = 0);
  102. template <int I>
  103. void i(int a = I);
  104. template <typename T>
  105. void h(T t) {
  106. f(0);
  107. g(1);
  108. g(t);
  109. i<2>();
  110. }
  111. void test() {
  112. h(0);
  113. }
  114. }
  115. // PR5810
  116. namespace PR5810 {
  117. template<typename T>
  118. struct allocator {
  119. allocator() { int a[sizeof(T) ? -1 : -1]; } // expected-error2 {{array with a negative size}}
  120. };
  121. template<typename T>
  122. struct vector {
  123. vector(const allocator<T>& = allocator<T>()) {} // expected-note2 {{instantiation of}}
  124. };
  125. struct A { };
  126. struct B { };
  127. template<typename>
  128. void FilterVTs() {
  129. vector<A> Result;
  130. }
  131. void f() {
  132. vector<A> Result;
  133. }
  134. template<typename T>
  135. struct X {
  136. vector<B> bs;
  137. X() { }
  138. };
  139. void f2() {
  140. X<float> x; // expected-note{{member function}}
  141. }
  142. }
  143. template<typename T> void f4(T, int = 17);
  144. template<> void f4<int>(int, int);
  145. void f4_test(int i) {
  146. f4(i);
  147. }
  148. // Instantiate for initialization
  149. namespace InstForInit {
  150. template<typename T>
  151. struct Ptr {
  152. typedef T* type;
  153. Ptr(type);
  154. };
  155. template<typename T>
  156. struct Holder {
  157. Holder(int i, Ptr<T> ptr = 0);
  158. };
  159. void test_holder(int i) {
  160. Holder<int> h(i);
  161. }
  162. };
  163. namespace PR5810b {
  164. template<typename T>
  165. T broken() {
  166. T t;
  167. double**** not_it = t;
  168. }
  169. void f(int = broken<int>());
  170. void g() { f(17); }
  171. }
  172. namespace PR5810c {
  173. template<typename T>
  174. struct X {
  175. X() {
  176. T t;
  177. double *****p = t; // expected-error{{cannot initialize a variable of type 'double *****' with an lvalue of type 'int'}}
  178. }
  179. X(const X&) { }
  180. };
  181. struct Y : X<int> { // expected-note{{instantiation of}}
  182. };
  183. void f(Y y = Y());
  184. void g() { f(); }
  185. }
  186. namespace PR8127 {
  187. template< typename T > class PointerClass {
  188. public:
  189. PointerClass( T * object_p ) : p_( object_p ) {
  190. p_->acquire();
  191. }
  192. private:
  193. T * p_;
  194. };
  195. class ExternallyImplementedClass;
  196. class MyClass {
  197. void foo( PointerClass<ExternallyImplementedClass> = 0 );
  198. };
  199. }
  200. namespace rdar8427926 {
  201. template<typename T>
  202. struct Boom {
  203. ~Boom() {
  204. T t;
  205. double *******ptr = t; // expected-error 2{{cannot initialize}}
  206. }
  207. };
  208. Boom<float> *bfp;
  209. struct X {
  210. void f(Boom<int> = Boom<int>()) { } // expected-note{{requested here}}
  211. void g(int x = (delete bfp, 0)); // expected-note{{requested here}}
  212. };
  213. void test(X *x) {
  214. x->f();
  215. x->g();
  216. }
  217. }
  218. namespace PR8401 {
  219. template<typename T>
  220. struct A {
  221. A() { T* x = 1; } // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
  222. };
  223. template<typename T>
  224. struct B {
  225. B(const A<T>& a = A<T>()); // expected-note{{in instantiation of}}
  226. };
  227. void f(B<int> b = B<int>());
  228. void g() {
  229. f();
  230. }
  231. }
  232. namespace PR12581 {
  233. const int a = 0;
  234. template < typename > struct A;
  235. template < typename MatrixType, int =
  236. A < MatrixType >::Flags ? : A < MatrixType >::Flags & a > class B;
  237. void
  238. fn1 ()
  239. {
  240. }
  241. }
  242. namespace PR13758 {
  243. template <typename T> struct move_from {
  244. T invalid;
  245. };
  246. template <class K>
  247. struct unordered_map {
  248. explicit unordered_map(int n = 42);
  249. unordered_map(move_from<K> other);
  250. };
  251. template<typename T>
  252. void StripedHashTable() {
  253. new unordered_map<void>();
  254. new unordered_map<void>;
  255. }
  256. void tt() {
  257. StripedHashTable<int>();
  258. }
  259. }