default-arguments.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template<typename T, int N = 2> struct X; // expected-note{{template is declared here}}
  3. X<int, 1> *x1;
  4. X<int> *x2;
  5. X<> *x3; // expected-error{{too few template arguments for class template 'X'}}
  6. template<typename U = float, int M> struct X;
  7. X<> *x4;
  8. template<typename T = int> struct Z { };
  9. template struct Z<>;
  10. // PR4362
  11. template<class T> struct a { };
  12. template<> struct a<int> { static const bool v = true; };
  13. template<class T, bool = a<T>::v> struct p { }; // expected-error {{no member named 'v'}}
  14. template struct p<bool>; // expected-note {{in instantiation of default argument for 'p<bool>' required here}}
  15. template struct p<int>;
  16. // PR5187
  17. template<typename T, typename U>
  18. struct A;
  19. template<typename T, typename U = T>
  20. struct A;
  21. template<typename T, typename U>
  22. struct A {
  23. void f(A<T>);
  24. };
  25. template<typename T>
  26. struct B { };
  27. template<>
  28. struct B<void> {
  29. typedef B<void*> type;
  30. };
  31. // Nested default arguments for template parameters.
  32. template<typename T> struct X1 { };
  33. template<typename T>
  34. struct X2 {
  35. template<typename U = typename X1<T>::type> // expected-error{{no type named 'type' in 'X1<int>'}} \
  36. // expected-error{{no type named 'type' in 'X1<char>'}}
  37. struct Inner1 { }; // expected-note{{template is declared here}}
  38. template<T Value = X1<T>::value> // expected-error{{no member named 'value' in 'X1<int>'}} \
  39. // expected-error{{no member named 'value' in 'X1<char>'}}
  40. struct NonType1 { }; // expected-note{{template is declared here}}
  41. template<T Value>
  42. struct Inner2 { };
  43. template<typename U>
  44. struct Inner3 {
  45. template<typename X = T, typename V = U>
  46. struct VeryInner { };
  47. template<T Value1 = sizeof(T), T Value2 = sizeof(U),
  48. T Value3 = Value1 + Value2>
  49. struct NonType2 { };
  50. };
  51. };
  52. X2<int> x2i; // expected-note{{in instantiation of template class 'X2<int>' requested here}}
  53. X2<int>::Inner1<float> x2iif;
  54. X2<int>::Inner1<> x2bad; // expected-error{{too few template arguments for class template 'Inner1'}}
  55. X2<int>::NonType1<'a'> x2_nontype1;
  56. X2<int>::NonType1<> x2_nontype1_bad; // expected-error{{too few template arguments for class template 'NonType1'}}
  57. // Check multi-level substitution into template type arguments
  58. X2<int>::Inner3<float>::VeryInner<> vi;
  59. X2<char>::Inner3<int>::NonType2<> x2_deep_nontype; // expected-note{{in instantiation of template class 'X2<char>' requested here}}
  60. template<typename T, typename U>
  61. struct is_same { static const bool value = false; };
  62. template<typename T>
  63. struct is_same<T, T> { static const bool value = true; };
  64. int array1[is_same<__typeof__(vi),
  65. X2<int>::Inner3<float>::VeryInner<int, float> >::value? 1 : -1];
  66. int array2[is_same<__typeof(x2_deep_nontype),
  67. X2<char>::Inner3<int>::NonType2<sizeof(char), sizeof(int),
  68. sizeof(char)+sizeof(int)> >::value? 1 : -1];
  69. // Template template parameter defaults
  70. template<template<typename T> class X = X2> struct X3 { };
  71. int array3[is_same<X3<>, X3<X2> >::value? 1 : -1];
  72. struct add_pointer {
  73. template<typename T>
  74. struct apply {
  75. typedef T* type;
  76. };
  77. };
  78. template<typename T, template<typename> class X = T::template apply>
  79. struct X4;
  80. int array4[is_same<X4<add_pointer>,
  81. X4<add_pointer, add_pointer::apply> >::value? 1 : -1];
  82. template<int> struct X5 {}; // expected-note{{has a different type 'int'}}
  83. template<long> struct X5b {};
  84. template<typename T,
  85. template<T> class B = X5> // expected-error{{template template argument has different}} \
  86. // expected-note{{previous non-type template parameter}}
  87. struct X6 {};
  88. X6<int> x6a;
  89. X6<long> x6b; // expected-note{{while checking a default template argument}}
  90. X6<long, X5b> x6c;
  91. template<template<class> class X = B<int> > struct X7; // expected-error{{must be a class template}}
  92. namespace PR9643 {
  93. template<typename T> class allocator {};
  94. template<typename T, typename U = allocator<T> > class vector {};
  95. template<template<typename U, typename = allocator<U> > class container,
  96. typename DT>
  97. container<DT> initializer(const DT& d) {
  98. return container<DT>();
  99. }
  100. void f() {
  101. vector<int, allocator<int> > v = initializer<vector>(5);
  102. }
  103. }
  104. namespace PR16288 {
  105. template<typename X>
  106. struct S {
  107. template<typename T = int, typename U> // expected-warning {{C++11}}
  108. void f();
  109. };
  110. template<typename X>
  111. template<typename T, typename U>
  112. void S<X>::f() {}
  113. }
  114. namespace DR1635 {
  115. template <class T> struct X {
  116. template <class U = typename T::type> static void f(int) {} // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} \
  117. // expected-warning {{C++11}}
  118. static void f(...) {}
  119. };
  120. int g() { X<int>::f(0); } // expected-note {{in instantiation of template class 'DR1635::X<int>' requested here}}
  121. }