class-template-spec.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template<typename T, typename U = int> struct A; // expected-note {{template is declared here}} \
  3. // expected-note{{explicitly specialized}}
  4. template<> struct A<double, double>; // expected-note{{forward declaration}}
  5. template<> struct A<float, float> { // expected-note{{previous definition}}
  6. int x;
  7. };
  8. template<> struct A<float> { // expected-note{{previous definition}}
  9. int y;
  10. };
  11. int test_specs(A<float, float> *a1, A<float, int> *a2) {
  12. return a1->x + a2->y;
  13. }
  14. int test_incomplete_specs(A<double, double> *a1,
  15. A<double> *a2)
  16. {
  17. (void)a1->x; // expected-error{{member access into incomplete type}}
  18. (void)a2->x; // expected-error{{implicit instantiation of undefined template 'A<double, int>'}}
  19. }
  20. typedef float FLOAT;
  21. template<> struct A<float, FLOAT>;
  22. template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
  23. template<> struct A<float, int> { }; // expected-error{{redefinition}}
  24. template<typename T, typename U = int> struct X;
  25. template <> struct X<int, int> { int foo(); }; // #1
  26. template <> struct X<float> { int bar(); }; // #2
  27. typedef int int_type;
  28. void testme(X<int_type> *x1, X<float, int> *x2) {
  29. (void)x1->foo(); // okay: refers to #1
  30. (void)x2->bar(); // okay: refers to #2
  31. }
  32. // Make sure specializations are proper classes.
  33. template<>
  34. struct A<char> {
  35. A();
  36. };
  37. A<char>::A() { }
  38. // Make sure we can see specializations defined before the primary template.
  39. namespace N{
  40. template<typename T> struct A0;
  41. }
  42. namespace N {
  43. template<>
  44. struct A0<void> {
  45. typedef void* pointer;
  46. };
  47. }
  48. namespace N {
  49. template<typename T>
  50. struct A0 {
  51. void foo(A0<void>::pointer p = 0);
  52. };
  53. }
  54. // Diagnose specialization errors
  55. struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
  56. template<> struct ::A<double>;
  57. namespace N {
  58. template<typename T> struct B; // expected-note 2{{explicitly specialized}}
  59. template<> struct ::N::B<char>; // okay
  60. template<> struct ::N::B<short>; // okay
  61. template<> struct ::N::B<int>; // okay
  62. int f(int);
  63. }
  64. template<> struct N::B<int> { }; // okay
  65. template<> struct N::B<float> { }; // expected-warning{{C++11 extension}}
  66. namespace M {
  67. template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
  68. template<> struct ::A<long double>; // expected-error{{must occur at global scope}}
  69. }
  70. template<> struct N::B<char> {
  71. int testf(int x) { return f(x); }
  72. };
  73. // PR5264
  74. template <typename T> class Foo;
  75. Foo<int>* v;
  76. Foo<int>& F() { return *v; }
  77. template <typename T> class Foo {};
  78. Foo<int> x;
  79. // Template template parameters
  80. template<template<class T> class Wibble>
  81. class Wibble<int> { }; // expected-error{{cannot specialize a template template parameter}}
  82. namespace rdar9676205 {
  83. template<typename T>
  84. struct X {
  85. template<typename U>
  86. struct X<U*> { // expected-error{{explicit specialization of 'X' in class scope}}
  87. };
  88. };
  89. }
  90. namespace PR18009 {
  91. template <typename T> struct A {
  92. template <int N, int M> struct S;
  93. template <int N> struct S<N, sizeof(T)> {};
  94. };
  95. A<int>::S<8, sizeof(int)> a; // ok
  96. template <typename T> struct B {
  97. template <int N, int M> struct S; // expected-note {{declared here}}
  98. template <int N> struct S<N, sizeof(T) +
  99. N // expected-error {{non-type template argument depends on a template parameter of the partial specialization}}
  100. > {};
  101. };
  102. B<int>::S<8, sizeof(int) + 8> s; // expected-error {{undefined}}
  103. template<int A> struct outer {
  104. template<int B, int C> struct inner {};
  105. template<int C> struct inner<A * 2, C> {};
  106. };
  107. }
  108. namespace PR16519 {
  109. template<typename T, T...N> struct integer_sequence { typedef T value_type; }; // expected-warning {{extension}}
  110. template<typename T> struct __make_integer_sequence;
  111. template<typename T, T N> using make_integer_sequence = typename __make_integer_sequence<T>::template make<N, N % 2>::type; // expected-warning {{extension}}
  112. template<typename T, typename T::value_type ...Extra> struct __make_integer_sequence_impl; // expected-warning {{extension}}
  113. template<typename T, T ...N, T ...Extra> struct __make_integer_sequence_impl<integer_sequence<T, N...>, Extra...> { // expected-warning 2{{extension}}
  114. typedef integer_sequence<T, N..., sizeof...(N) + N..., Extra...> type;
  115. };
  116. template<typename T> struct __make_integer_sequence {
  117. template<T N, T Parity, typename = void> struct make;
  118. template<typename Dummy> struct make<0, 0, Dummy> { typedef integer_sequence<T> type; };
  119. template<typename Dummy> struct make<1, 1, Dummy> { typedef integer_sequence<T, 0> type; };
  120. template<T N, typename Dummy> struct make<N, 0, Dummy> : __make_integer_sequence_impl<make_integer_sequence<T, N/2> > {};
  121. template<T N, typename Dummy> struct make<N, 1, Dummy> : __make_integer_sequence_impl<make_integer_sequence<T, N/2>, N - 1> {};
  122. };
  123. using X = make_integer_sequence<int, 5>; // expected-warning {{extension}}
  124. using X = integer_sequence<int, 0, 1, 2, 3, 4>; // expected-warning {{extension}}
  125. }
  126. namespace DefaultArgVsPartialSpec {
  127. // Check that the diagnostic points at the partial specialization, not just at
  128. // the default argument.
  129. template<typename T, int N =
  130. sizeof(T) // expected-note {{template parameter is used in default argument declared here}}
  131. > struct X {};
  132. template<typename T> struct X<T> {}; // expected-error {{non-type template argument depends on a template parameter of the partial specialization}}
  133. template<typename T,
  134. T N = 0 // expected-note {{template parameter is declared here}}
  135. > struct S;
  136. template<typename T> struct S<T> {}; // expected-error {{non-type template argument specializes a template parameter with dependent type 'T'}}
  137. }